jackyfkc.github.io

教土豆学计算机

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:

Anatomy

To minimize the cost of concurrent connections, Tornado uses a single-threaded event loop.

Concept

There are many styles of asynchronous interfaces

Coroutine

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)

Changelog In Github

FAQ