From 15a9e38b1c4626e740f432ea906269198a94f56e Mon Sep 17 00:00:00 2001 From: Yury Selivanov <yselivanov@sprymix.com> Date: Tue, 14 Jan 2014 11:20:46 -0500 Subject: [PATCH] Fix examples to work with asyncio --- examples/httpget.py | 16 ++++++++-------- examples/mysql.py | 16 ++++++++-------- examples/postgres.py | 16 ++++++++-------- examples/simple.py | 12 ++++++------ 4 files changed, 30 insertions(+), 30 deletions(-) diff --git a/examples/httpget.py b/examples/httpget.py index 9908bca..0991e34 100644 --- a/examples/httpget.py +++ b/examples/httpget.py @@ -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())) diff --git a/examples/mysql.py b/examples/mysql.py index 6e80b87..f26b0b5 100644 --- a/examples/mysql.py +++ b/examples/mysql.py @@ -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())) diff --git a/examples/postgres.py b/examples/postgres.py index 10d4af7..cac0f77 100644 --- a/examples/postgres.py +++ b/examples/postgres.py @@ -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())) diff --git a/examples/simple.py b/examples/simple.py index 76543c6..dd8beb2 100644 --- a/examples/simple.py +++ b/examples/simple.py @@ -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())