Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Tornado really non-blocking?

Tornado advertises itself as "a relatively simple, non-blocking web server framework" and was designed to solve the C10k problem. However, looking at their database wrapper, which wraps MySQLdb, I came across the following piece of code:

def _execute(self, cursor, query, parameters):
    try:
        return cursor.execute(query, parameters)
    except OperationalError:
        logging.error("Error connecting to MySQL on %s", self.host)
        self.close()
        raise

As far as I know calls to the MySQLdb, which is built on top of libmysqlclient, are blocking.

Am I right in thinking that a long-running query would render the entire Tornado server unresponsive until it finishes or is there magic on the code?

like image 393
ipartola Avatar asked Sep 03 '10 19:09

ipartola


People also ask

Is tornado single threaded?

The reason is that Tornado is an asynchronous server with only one thread.

Is tornado asynchronous?

Tornado is a Python web framework and asynchronous networking library, originally developed at FriendFeed.

Is asynchronous and non blocking same?

Non-Blocking: It refers to the program that does not block the execution of further operations. Non-Blocking methods are executed asynchronously. Asynchronously means that the program may not necessarily execute line by line.


2 Answers

Tornado is non-blocking if you write non-blocking code on the top if it, eg. using asyncmongo and @tornado.web.asynchronous decorator. Tornado as a framework provides tools for that.

Bret Taylor, one of the original authors, writes:

We experimented with different async DB approaches, but settled on synchronous at FriendFeed because generally if our DB queries were backlogging our requests, our backends couldn't scale to the load anyway. Things that were slow enough were abstracted to separate backend services which we fetched asynchronously via the async HTTP module.

It's true that Tornado doesn't include a non-blocking database layer; in fact the database layer is not integral part of the Tornado framework at all, as opposed to e.g. Django's ORM. Yes, Tornado ships with blocking MySQL wrapper because that's what FriendFeed happened to use, but it's more an external library than core functionality. I'm pretty sure most people are using something else for database access.

like image 76
jholster Avatar answered Oct 21 '22 10:10

jholster


Yes, absent other measures, the server will wait for the query to finish executing. That does not mean Tornado is not a non-blocking web server.

A "non-blocking web server" doesn't block on network I/O (and may have some provision for disk I/O if it does static file serving). That does not mean you get instant, causality-violating instruction execution in your application.

Doing a database call takes time, just like reading files, formatting strings, processing templates, etc. takes time. Doing any of these things in the same thread as the server's main event loop will prevent the loop from moving on until you're done.

like image 20
Nicholas Knight Avatar answered Oct 21 '22 09:10

Nicholas Knight