summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2014-06-11 17:29:29 +0200
committerVictor Stinner <victor.stinner@gmail.com>2014-06-11 17:29:29 +0200
commit827421b2b8a4bed339a5d766db7c0e286e08238a (patch)
tree9f7cf75f64f461daa052cfe16d0724d44bd5186b
parentc599890b99f202f7a26bfdf1b1be01cb96159d7f (diff)
downloadtrollius-827421b2b8a4bed339a5d766db7c0e286e08238a.tar.gz
Add an example of interoperability between Trollius and asyncio
-rw-r--r--examples/interop_asyncio.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/examples/interop_asyncio.py b/examples/interop_asyncio.py
new file mode 100644
index 0000000..a0cbc3f
--- /dev/null
+++ b/examples/interop_asyncio.py
@@ -0,0 +1,51 @@
+import asyncio
+import trollius
+
+@asyncio.coroutine
+def asyncio_noop():
+ pass
+
+@asyncio.coroutine
+def asyncio_coroutine(coro):
+ print("asyncio coroutine")
+ res = yield from coro
+ print("asyncio inner coroutine result: %r" % (res,))
+ print("asyncio coroutine done")
+ return "asyncio"
+
+@trollius.coroutine
+def trollius_noop():
+ pass
+
+@trollius.coroutine
+def trollius_coroutine(coro):
+ print("trollius coroutine")
+ res = yield trollius.From(coro)
+ print("trollius inner coroutine result: %r" % (res,))
+ print("trollius coroutine done")
+ raise trollius.Return("trollius")
+
+def main():
+ # create an event loop for the main thread: use Trollius event loop
+ loop = trollius.get_event_loop()
+
+ # set the asyncio event loop (for the main thread)
+ asyncio.set_event_loop(loop)
+
+ print("[ asyncio coroutine called from trollius coroutine ]")
+ coro1 = asyncio_noop()
+ coro2 = asyncio_coroutine(coro1)
+ res = loop.run_until_complete(trollius_coroutine(coro2))
+ print("trollius coroutine result: %r" % res)
+ print("")
+
+ print("[ asyncio coroutine called from trollius coroutine ]")
+ coro1 = trollius_noop()
+ coro2 = trollius_coroutine(coro1)
+ res = loop.run_until_complete(asyncio_coroutine(coro2))
+ print("asyncio coroutine result: %r" % res)
+ print("")
+
+ loop.close()
+
+main()