tornado.platform.twisted
— Bridges between Twisted and Tornado¶
Bridges between the Twisted reactor and Tornado IOLoop.
This module lets you run applications and libraries written for Twisted in a Tornado application. It can be used in two modes, depending on which library’s underlying event loop you want to use.
This module has been tested with Twisted versions 11.0.0 and newer.
Twisted on Tornado¶
TornadoReactor
implements the Twisted reactor interface on top of
the Tornado IOLoop. To use it, simply call install
at the beginning
of the application:
import tornado.platform.twisted
tornado.platform.twisted.install()
from twisted.internet import reactor
When the app is ready to start, call IOLoop.current().start()
instead of reactor.run()
.
It is also possible to create a non-global reactor by calling
tornado.platform.twisted.TornadoReactor(io_loop)
. However, if
the IOLoop
and reactor are to be short-lived (such as those used in
unit tests), additional cleanup may be required. Specifically, it is
recommended to call:
reactor.fireSystemEvent('shutdown')
reactor.disconnectAll()
before closing the IOLoop
.
Tornado on Twisted¶
TwistedIOLoop
implements the Tornado IOLoop interface on top of the Twisted
reactor. Recommended usage:
from tornado.platform.twisted import TwistedIOLoop
from twisted.internet import reactor
TwistedIOLoop().install()
# Set up your tornado application as usual using `IOLoop.instance`
reactor.run()
TwistedIOLoop
always uses the global Twisted reactor.
Twisted on Tornado¶
-
class
tornado.platform.twisted.
TornadoReactor
(io_loop=None)[source]¶ Twisted reactor built on the Tornado IOLoop.
Since it is intended to be used in applications where the top-level event loop is
io_loop.start()
rather thanreactor.run()
, it is implemented a little differently than other Twisted reactors. We overridemainLoop
instead ofdoIteration
and must implement timed call functionality on top ofIOLoop.add_timeout
rather than using the implementation inPosixReactorBase
.Changed in version 4.1: The
io_loop
argument is deprecated.
Tornado on Twisted¶
-
class
tornado.platform.twisted.
TwistedIOLoop
[source]¶ IOLoop implementation that runs on Twisted.
Uses the global Twisted reactor by default. To create multiple
TwistedIOLoops
in the same process, you must pass a unique reactor when constructing each one.Not compatible with
tornado.process.Subprocess.set_exit_callback
because theSIGCHLD
handlers used by Tornado and Twisted conflict with each other.
Twisted DNS resolver¶
-
class
tornado.platform.twisted.
TwistedResolver
[source]¶ Twisted-based asynchronous resolver.
This is a non-blocking and non-threaded resolver. It is recommended only when threads cannot be used, since it has limitations compared to the standard
getaddrinfo
-basedResolver
andThreadedResolver
. Specifically, it returns at most one result, and arguments other thanhost
andfamily
are ignored. It may fail to resolve whenfamily
is notsocket.AF_UNSPEC
.Requires Twisted 12.1 or newer.
Changed in version 4.1: The
io_loop
argument is deprecated.