]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Add bidirectional asyncio future conversion.
authorBen Darnell <ben@bendarnell.com>
Mon, 19 Jan 2015 16:43:52 +0000 (11:43 -0500)
committerBen Darnell <ben@bendarnell.com>
Mon, 19 Jan 2015 16:43:52 +0000 (11:43 -0500)
tornado/platform/asyncio.py

index 5842d108c92e09d62f96b03fa704afd2b6b56676..bc6851750ac119e8e22de79e9a5ffa3bc000dde5 100644 (file)
@@ -141,10 +141,17 @@ class AsyncIOLoop(BaseAsyncIOLoop):
         super(AsyncIOLoop, self).initialize(asyncio.new_event_loop(),
                                             close_loop=True)
 
+def to_tornado_future(asyncio_future):
+    """Convert an ``asyncio.Future`` to a `tornado.concurrent.Future`."""
+    tf = tornado.concurrent.Future()
+    tornado.concurrent.chain_future(asyncio_future, tf)
+    return tf
+
+def to_asyncio_future(tornado_future):
+    """Convert a `tornado.concurrent.Future` to an ``asyncio.Future``."""
+    af = asyncio.Future()
+    tornado.concurrent.chain_future(tornado_future, af)
+    return af
 
 if hasattr(convert_yielded, 'register'):
-    @convert_yielded.register(asyncio.Future)
-    def _(af):
-        tf = tornado.concurrent.Future()
-        tornado.concurrent.chain_future(af, tf)
-        return tf
+    convert_yielded.register(asyncio.Future, to_tornado_future)