Fix examples to work with asyncio

This commit is contained in:
Yury Selivanov 2014-01-14 11:20:46 -05:00
parent e9c4bc3b49
commit 15a9e38b1c
4 changed files with 30 additions and 30 deletions

@ -5,13 +5,13 @@
import greentulip
import tulip
import asyncio
@tulip.task
@asyncio.coroutine
def sleeper():
while True:
yield from tulip.sleep(0.05)
yield from asyncio.sleep(0.05)
print('.')
@ -27,11 +27,11 @@ def get():
sock.close()
@tulip.task
@asyncio.coroutine
def run():
yield from tulip.wait(
[get(), sleeper()], return_when=tulip.FIRST_COMPLETED)
yield from asyncio.wait(
[get(), sleeper()], return_when=asyncio.FIRST_COMPLETED)
tulip.set_event_loop_policy(greentulip.GreenEventLoopPolicy())
tulip.get_event_loop().run_until_complete(run())
asyncio.set_event_loop_policy(greentulip.GreenEventLoopPolicy())
asyncio.get_event_loop().run_until_complete(asyncio.Task(run()))

@ -1,5 +1,5 @@
"""PyMySQL example"""
import tulip
import asyncio
import socket
from pymysql import connections
from greentulip import socket as greensocket
@ -33,11 +33,11 @@ if __name__ == '__main__':
import greentulip
import time
@tulip.task
@asyncio.coroutine
def sleeper():
# show that we're not blocked
while True:
yield from tulip.sleep(0.2)
yield from asyncio.sleep(0.2)
print('.')
@greentulip.task
@ -65,10 +65,10 @@ if __name__ == '__main__':
finally:
conn.close()
@tulip.task
@asyncio.coroutine
def run():
yield from tulip.wait([db(), sleeper()],
return_when=tulip.FIRST_COMPLETED)
yield from asyncio.wait([db(), sleeper()],
return_when=asyncio.FIRST_COMPLETED)
tulip.set_event_loop_policy(greentulip.GreenEventLoopPolicy())
tulip.get_event_loop().run_until_complete(run())
asyncio.set_event_loop_policy(greentulip.GreenEventLoopPolicy())
asyncio.get_event_loop().run_until_complete(asyncio.Task(run()))

@ -94,13 +94,13 @@ def connector_factory(iri, async=False):
if __name__ == '__main__':
import greentulip
import time
import tulip
import asyncio
@tulip.task
@asyncio.coroutine
def sleeper():
# show that we're not blocked
while True:
yield from tulip.sleep(0.4)
yield from asyncio.sleep(0.4)
print('.')
@greentulip.task
@ -122,10 +122,10 @@ if __name__ == '__main__':
finally:
connection.close()
@tulip.task
@asyncio.coroutine
def run():
yield from tulip.wait(
[db(), sleeper()], return_when=tulip.FIRST_COMPLETED)
yield from asyncio.wait(
[db(), sleeper()], return_when=asyncio.FIRST_COMPLETED)
tulip.set_event_loop_policy(greentulip.GreenEventLoopPolicy())
tulip.get_event_loop().run_until_complete(run())
asyncio.set_event_loop_policy(greentulip.GreenEventLoopPolicy())
asyncio.get_event_loop().run_until_complete(asyncio.Task(run()))

@ -5,18 +5,18 @@
import greentulip
import tulip
import asyncio
@tulip.task
@asyncio.coroutine
def bar():
yield
return 30
@tulip.coroutine
@asyncio.coroutine
def foo():
bar_result = greentulip.yield_from(bar())
bar_result = greentulip.yield_from(asyncio.Task(bar()))
return bar_result + 12
@ -25,5 +25,5 @@ def test():
print((yield from foo()))
tulip.set_event_loop_policy(greentulip.GreenEventLoopPolicy())
tulip.get_event_loop().run_until_complete(test())
asyncio.set_event_loop_policy(greentulip.GreenEventLoopPolicy())
asyncio.get_event_loop().run_until_complete(test())