Reputation: 262
In async web frameworks like Tornado, we can do non-blocking calls using its @gen.coroutine or @tornado.web.asynchronous decorators. For example we can do non-blocking or async calls by using the AsyncHTTPClient module.
So we can use it either by using @gen.coroutine decorator along with the "yield" keyword, like "yield tornado.httpclient.AsyncHTTPClient().fetch(url)" or by using a callback function with @tornado.web.asynchronous. For both of the methods, it works as non-blocking.
Similarly, then why can't it be done the same way for Python's MySQL drivers/clients like MySQLdb, PyMySQL, mysqlclient etc. I mean why can't these drivers be used similarly to how AsyncHTTPClient is used along with either gen.coroutine or callback via web.asynchronous decorators. Why can't the drivers be used with gen.coroutine or web.asynchronous to make the calls non-blocking?
What are the reasons that stop them from being asynchronous, I mean how AsyncHTTPClient is non-blocking but MySQLdb.connect, MySQLdb.connect().cursor.query(), etc are not?
Also I know there are few async Tornado specific MySQL drivers/clients like TorMySQL, Tornado-MySQL (by PyMySQL), asynctorndb, etc. But why can't the normal drivers/clients like mysqlclient or MySQLdb be used directly as non-blocking, or what are the changes made in TorMySQL, Tornado-MySQL (by PyMySQL) that they are defined as async mysql drivers/clients for Tornado?
Upvotes: 0
Views: 655
Reputation: 22154
When we say that Tornado is an asynchronous framework, this is a requirement rather than an effect. To use Tornado effectively, anything you do that is expensive needs to be asynchronous; Tornado cannot magically make synchronous code asynchronous. This means you need to either choose asynchronous libraries which are compatible with Tornado, or run expensive blocking operations in another thread.
See the introduction to asynchronous I/O in the Tornado User's guide for more details.
Upvotes: 2