tornado.platform.asyncio — Bridge between asyncio and Tornado¶
Bridges between the asyncio module and Tornado IOLoop.
New in version 3.2.
This module integrates Tornado with the asyncio module introduced
in Python 3.4 (and available as a separate download for Python 3.3). This makes
it possible to combine the two libraries on the same event loop.
Most applications should use AsyncIOMainLoop to run Tornado on the
default asyncio event loop. Applications that need to run event
loops on multiple threads may use AsyncIOLoop to create multiple
loops.
Note
Tornado requires the add_reader family of
methods, so it is not compatible with the ProactorEventLoop on
Windows. Use the SelectorEventLoop instead.
-
class
tornado.platform.asyncio.BaseAsyncIOLoop[source]¶ -
initialize(asyncio_loop, close_loop=False, **kwargs)[source]¶ Initialize a
Configurablesubclass instance.Configurable classes should use
initializeinstead of__init__.Changed in version 4.2: Now accepts positional arguments in addition to keyword arguments.
-
close(all_fds=False)[source]¶ Closes the
IOLoop, freeing any resources used.If
all_fdsis true, all file descriptors registered on the IOLoop will be closed (not just the ones created by theIOLoopitself).Many applications will only use a single
IOLoopthat runs for the entire lifetime of the process. In that case closing theIOLoopis not necessary since everything will be cleaned up when the process exits.IOLoop.closeis provided mainly for scenarios such as unit tests, which create and destroy a large number ofIOLoops.An
IOLoopmust be completely stopped before it can be closed. This means thatIOLoop.stop()must be called andIOLoop.start()must be allowed to return before attempting to callIOLoop.close(). Therefore the call toclosewill usually appear just after the call tostartrather than near the call tostop.Changed in version 3.1: If the
IOLoopimplementation supports non-integer objects for “file descriptors”, those objects will have theirclosemethod whenall_fdsis true.
-
add_handler(fd, handler, events)[source]¶ Registers the given handler to receive the given events for
fd.The
fdargument may either be an integer file descriptor or a file-like object with afileno()method (and optionally aclose()method, which may be called when theIOLoopis shut down).The
eventsargument is a bitwise or of the constantsIOLoop.READ,IOLoop.WRITE, andIOLoop.ERROR.When an event occurs,
handler(fd, events)will be run.Changed in version 4.0: Added the ability to pass file-like objects in addition to raw file descriptors.
-
update_handler(fd, events)[source]¶ Changes the events we listen for
fd.Changed in version 4.0: Added the ability to pass file-like objects in addition to raw file descriptors.
-
remove_handler(fd)[source]¶ Stop listening for events on
fd.Changed in version 4.0: Added the ability to pass file-like objects in addition to raw file descriptors.
-
start()[source]¶ Starts the I/O loop.
The loop will run until one of the callbacks calls
stop(), which will make the loop stop after the current event iteration completes.
-
stop()[source]¶ Stop the I/O loop.
If the event loop is not currently running, the next call to
start()will return immediately.To use asynchronous methods from otherwise-synchronous code (such as unit tests), you can start and stop the event loop like this:
ioloop = IOLoop() async_method(ioloop=ioloop, callback=ioloop.stop) ioloop.start()
ioloop.start()will return afterasync_methodhas run its callback, whether that callback was invoked before or afterioloop.start.Note that even after
stophas been called, theIOLoopis not completely stopped untilIOLoop.starthas also returned. Some work that was scheduled before the call tostopmay still be run before theIOLoopshuts down.
-
call_at(when, callback, *args, **kwargs)[source]¶ Runs the
callbackat the absolute time designated bywhen.whenmust be a number using the same reference point asIOLoop.time.Returns an opaque handle that may be passed to
remove_timeoutto cancel. Note that unlike theasynciomethod of the same name, the returned object does not have acancel()method.See
add_timeoutfor comments on thread-safety and subclassing.New in version 4.0.
-
remove_timeout(timeout)[source]¶ Cancels a pending timeout.
The argument is a handle as returned by
add_timeout. It is safe to callremove_timeouteven if the callback has already been run.
-
add_callback(callback, *args, **kwargs)[source]¶ Calls the given callback on the next I/O loop iteration.
It is safe to call this method from any thread at any time, except from a signal handler. Note that this is the only method in
IOLoopthat makes this thread-safety guarantee; all other interaction with theIOLoopmust be done from thatIOLoop’s thread.add_callback()may be used to transfer control from other threads to theIOLoop’s thread.To add a callback from a signal handler, see
add_callback_from_signal.
-
add_callback_from_signal(callback, *args, **kwargs)¶ Calls the given callback on the next I/O loop iteration.
It is safe to call this method from any thread at any time, except from a signal handler. Note that this is the only method in
IOLoopthat makes this thread-safety guarantee; all other interaction with theIOLoopmust be done from thatIOLoop’s thread.add_callback()may be used to transfer control from other threads to theIOLoop’s thread.To add a callback from a signal handler, see
add_callback_from_signal.
-
-
class
tornado.platform.asyncio.AsyncIOMainLoop[source]¶ AsyncIOMainLoopcreates anIOLoopthat corresponds to the currentasyncioevent loop (i.e. the one returned byasyncio.get_event_loop()). Recommended usage:from tornado4.platform.asyncio import AsyncIOMainLoop import asyncio AsyncIOMainLoop().install() asyncio.get_event_loop().run_forever()
See also
tornado4.ioloop.IOLoop.install()for general notes on installing alternative IOLoops.-
initialize(**kwargs)[source]¶ Initialize a
Configurablesubclass instance.Configurable classes should use
initializeinstead of__init__.Changed in version 4.2: Now accepts positional arguments in addition to keyword arguments.
-
-
class
tornado.platform.asyncio.AsyncIOLoop[source]¶ AsyncIOLoopis anIOLoopthat runs on anasyncioevent loop. This class follows the usual Tornado semantics for creating newIOLoops; these loops are not necessarily related to theasynciodefault event loop. Recommended usage:from tornado4.ioloop import IOLoop IOLoop.configure('tornado4.platform.asyncio.AsyncIOLoop') IOLoop.current().start()
Each
AsyncIOLoopcreates a newasyncio.EventLoop; this object can be accessed with theasyncio_loopattribute.-
initialize(**kwargs)[source]¶ Initialize a
Configurablesubclass instance.Configurable classes should use
initializeinstead of__init__.Changed in version 4.2: Now accepts positional arguments in addition to keyword arguments.
-
-
tornado.platform.asyncio.to_tornado_future(asyncio_future)[source]¶ Convert an
asyncio.Futureto atornado4.concurrent.Future.New in version 4.1.
-
tornado.platform.asyncio.to_asyncio_future(tornado_future)[source]¶ Convert a Tornado yieldable object to an
asyncio.Future.New in version 4.1.
Changed in version 4.3: Now accepts any yieldable object, not just
tornado4.concurrent.Future.