教土豆学计算机
Tornado is a Python web framework and asynchronous networking library, originally developed at FriendFeed; ideal for long polling
, WebSockets
, and other applications that require a long-lived connection to each user.
Tornado 是一个 Python Web 框架, 同时也是一个异步网络库. Tornado 适用于 Long polling
, WebSockets
以及需要长连接的场景.
Tornado can be roughly divided into four major components:
A web framework (including RequestHandler
which is sub-classed to create web applications, and various supporting classes).
Client- and server-side implementations of HTTP (HTTPServer
and AsyncHTTPClient
).
An asynchronous networking library including the classes IOLoop
and IOStream
, which serve as the building blocks for the HTTP components and can also be used to implement other protocols.
A coroutine library (tornado.gen
) which allows asynchronous code to be written in a more straightforward way than chaining callbacks.
To minimize the cost of concurrent connections, Tornado uses a single-threaded event loop
.
…
There are many styles of asynchronous interfaces
Callback argument
Return a placeholder (Future, Promise, Deferred)
Deliver to a queue
Callback registry (e.g. POSIX signals)
Coroutines are the recommended way to write asynchronous code in Tornado. Coroutines use the Python yield
keyword to suspend and resume execution instead of a chain of callbacks
Here is a highly simplified version of the coroutine decorator’s inner loop
# Simplified inner loop of tornado.gen.Runner
def run(self):
# send(x) makes the current yield return x.
# It returns when the next yield is reached
future = self.gen.send(self.next)
def callback(f):
self.next = f.result()
self.run()
future.add_done_callback(callback)