What is Tornado?
By using non-blocking network I/O, Tornado can scale to tens of thousands of open connections, making it ideal for long polling, WebSockets, and other applications that require a long-lived connection to each user.
Tornado is a Python web framework and asynchronous networking library, originally developed at FriendFeed. By using non-blocking network I/O, Tornado can scale to tens of thousands of open connections, making it ideal for long polling, WebSockets, and other applications that require a long-lived connection to each user.
Hello, world
Here is a simple “Hello, world” example web app for Tornado:
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
Threads and WSGI
Tornado is different from most Python web frameworks. It is not based on WSGI, and it is typically run with only one thread per process. See the User’s guide for more on Tornado’s approach to asynchronous programming.
While some support of WSGI is available in the tornado.wsgi
module, it is not a focus of development and most applications should be written to use Tornado’s own interfaces (such as tornado.web
) directly instead of using WSGI.
In general, Tornado code is not thread-safe. The only method in Tornado that is safe to call from other threads is IOLoop.add_callback
. You can also use IOLoop.run_in_executor
to asynchronously run a blocking function on another thread, but note that the function passed to run_in_executor
should avoid referencing any Tornado objects. run_in_executor
is the recommended way to interact with blocking code.
asyncio
Integration
Tornado is integrated with the standard library asyncio module and shares the same event loop (by default since Tornado 5.0). In general, libraries designed for use with asyncio can be mixed freely with Tornado.
official tornadoweb.org
src stackshare.io/tornado