From: Ben Darnell Date: Mon, 19 Jan 2015 16:43:52 +0000 (-0500) Subject: Add bidirectional asyncio future conversion. X-Git-Tag: v4.1.0b1~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=531c798acc1cfe766f33421663e5f6cc234c8f79;p=thirdparty%2Ftornado.git Add bidirectional asyncio future conversion. --- diff --git a/tornado/platform/asyncio.py b/tornado/platform/asyncio.py index 5842d108c..bc6851750 100644 --- a/tornado/platform/asyncio.py +++ b/tornado/platform/asyncio.py @@ -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)