Search code examples
pythonmultithreadingtornadononblockingsingle-threaded

Tornado non-blocking while run in single thread?


Tornado is non-blocking webserver.

However, all of the operations are run in a single thread. How does it stay non-blocking if it is handled by single thread?

If there is a long operation, will it block new coming request?

Is downloading a large file from Tornado a long blocking process?

Please kindly correct me if my understanding is not accurate.

Many Thanks


Solution

  • If there is a long operation, will it block new coming request?

    Yes. No. It depends.

    Anything which happens inside Tornado itself blocks. So if you do "time.sleep(10)" or do a computationally intensive operation, it will block.

    What Tornado (and Twisted, and node.js) can do well is request data from another service (like Amazon, or Facebook, or a subprocess, or a database with an async library) then serve other requests while it's waiting for a reply. See http://www.tornadoweb.org/documentation/overview.html#non-blocking-asynchronous-requests

    To do this, you need the server in front to be async too (so Nginx, not Apache).