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 forTwisted 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¶
- class
tornado.platform.twisted.
TornadoReactor
(io_loop=None)[源代码]¶
Twisted reactor built on the Tornado IOLoop.TornadoReactor
implements the Twisted reactor interface on top ofthe Tornado IOLoop. To use it, simply callinstall
at the beginningof the application:- import tornado.platform.twisted
tornado.platform.twisted.install()
from twisted.internet import reactor
When the app is ready to start, callIOLoop.current().start()
instead ofreactor.run()
.
It is also possible to create a non-global reactor by callingtornado.platform.twisted.TornadoReactor(ioloop)
. However, iftheIOLoop
and reactor are to be short-lived (such as those used inunit tests), additional cleanup may be required. Specifically, it isrecommended to call:- reactor.fireSystemEvent('shutdown')
reactor.disconnectAll()
before closing theIOLoop
.
在 4.1 版更改: Theio_loop
argument is deprecated.- import tornado.platform.twisted
tornado.platform.twisted.
install
(_io_loop=None)[源代码]¶
Install this package as the default Twisted reactor.install()
must be called very early in the startup process,before most other twisted-related imports. Conversely, because itinitializes theIOLoop
, it cannot be called beforefork_processes
or multi-processstart
. Theseconflicting requirements make it difficult to useTornadoReactor
in multi-process mode, and an external process manager such assupervisord
is recommended instead.
在 4.1 版更改: Theio_loop
argument is deprecated.
Tornado on Twisted¶
- class
tornado.platform.twisted.
TwistedIOLoop
[源代码]¶
IOLoop implementation that runs on Twisted.TwistedIOLoop
implements the Tornado IOLoop interface on top ofthe Twisted reactor. Recommended usage:- from tornado.platform.twisted import TwistedIOLoop
from twisted.internet import reactor
TwistedIOLoop().install()
# Set up your tornado application as usual usingIOLoop.instance
reactor.run()
Uses the global Twisted reactor by default. To create multipleTwistedIOLoops
in the same process, you must pass a unique reactorwhen constructing each one.
Not compatible withtornado.process.Subprocess.set_exit_callback
because theSIGCHLD
handlers used by Tornado and Twisted conflictwith each other.
See alsotornado.ioloop.IOLoop.install()
for general notes oninstalling alternative IOLoops.- from tornado.platform.twisted import TwistedIOLoop
Twisted DNS resolver¶
- class
tornado.platform.twisted.
TwistedResolver
[源代码]¶
Twisted-based asynchronous resolver.
This is a non-blocking and non-threaded resolver. It isrecommended only when threads cannot be used, since it haslimitations compared to the standardgetaddrinfo
-basedResolver
andThreadedResolver
. Specifically, it returns atmost 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.
在 4.1 版更改: Theio_loop
argument is deprecated.
原文:
https://tornado-zh-cn.readthedocs.io/zh_CN/latest/twisted.html