Search code examples
pythontornado

Tornado async basic concepts


I'm trying to understand some concepts of Tornado and async. If we have two sessions X and Y connected to the Tornado server T.

Now suppose X performs a database write that is blocking for 10seconds. In that period Y requests something from T. Does T have to wait until X is done with the write before being serviced?

I thought that tornado would spawn two instances and treat them separately switching from X to Y?


Solution

  • Whether or not tornado can handle the database connection in an asynchronous fashion depends on the database library you're using. For database queries to be performed asynchronously they must use Tornado's IOLoop, if they don't then Tornado will block while the database query is sent and a response received.

    So in your example if the database library doesn't use the underlying Tornado IOLoop then X will block for 10 seconds and Y (and any other request) will execute after the database write in X completes, however if the database library does use the Tornado IOLoop and you implement X as an asynchronous handler that yields when performing the database write, Y can be processed while Tornado's IOLoop waits for a response from the database for X's write db request.

    As an example for MongoDB there are a couple of ORM libraries I've used with Tornado, MotorEngine (https://motorengine.readthedocs.org) and MongoEngine (https://mongoengine-odm.readthedocs.org). MongoEngine doesn't use the Tornado IOLoop and so all calls are blocking, MotorEngine does use the IOLoop and so you can write queries that don't block.

    There are other ways to handle long running tasks (in the past I've set up additional web services and called them using the AsyncHTTPClient, I've also used a task scheduler such as Celery to push the processing into the background, often on to another server). In my experience however database queries are rarely a bottle neck and a good rule of thumb is to try to make sure you keep them as fast as possible, if you can't however hopefully my answer is useful.

    Re. I thought that tornado would spawn two instances and treat them separately switching from X to Y?

    Tornado does not spawn multiple instances to handle additional requests, you can run multiple instances of Tornado (in fact in my experience and per the example in the Tornado docs - this is advisable).