Skip to content

Instantly share code, notes, and snippets.

@AndreiPashkin
Last active August 29, 2015 14:10
Show Gist options
  • Save AndreiPashkin/844d17f2ab4f4c88be42 to your computer and use it in GitHub Desktop.
Save AndreiPashkin/844d17f2ab4f4c88be42 to your computer and use it in GitHub Desktop.
Bare coroutine vs Task
#!/usr/bin/env python
# coding=utf-8
import asyncio
import time
@asyncio.coroutine
def coro():
while True:
print('I\'m running!')
yield from asyncio.sleep(0)
@asyncio.coroutine
def background_stuff2():
print('Other background stuff...')
@asyncio.coroutine
def background_stuff():
while True:
time.sleep(0.5)
yield from background_stuff2()
time.sleep(0.5)
print('Other background stuff...')
loop = asyncio.get_event_loop()
asyncio.async(coro())
asyncio.async(background_stuff())
loop.run_forever()
#!/usr/bin/env python
# coding=utf-8
import trollius as asyncio
from trollius import From
import time
@asyncio.coroutine
def coro():
while True:
print('I\'m running!')
yield From(asyncio.sleep(0))
@asyncio.coroutine
def background_stuff2():
print('Other background stuff...')
@asyncio.coroutine
def background_stuff():
while True:
time.sleep(0.5)
yield From(background_stuff2())
time.sleep(0.5)
print('Other background stuff...')
loop = asyncio.get_event_loop()
asyncio.async(coro())
asyncio.async(background_stuff())
loop.run_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment