Last active
August 29, 2015 14:10
-
-
Save AndreiPashkin/844d17f2ab4f4c88be42 to your computer and use it in GitHub Desktop.
Bare coroutine vs Task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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