Search code examples
pythondatabaseasynchronousiotornado

Any suggestion for using non-blocking MySQL api on Tornado in Python3?


i was hoping tornado support for asynchronous sql database opertion ,after i read the source code,

http://www.tornadoweb.org/documentation/_modules/tornado/database.html#Connection

sigh,they are blocking version.here's some choice .

Plan A: find a set of api similar to mysqldb module except they use callback to return the reuslt.sorry i didn't find one that show examples that's their api can handle non-blocking mysql operation

Plan B: use the block version .i heard the writer of tornado suggest developers use block version,and extremely optimize your sql query for shorten the blocking period.

in my case: the server need to handle receive 2k request per second in peak hour and most of them need query and update from database ,suggest every operation take 10ms,10ms*2k over 20s ,that would be a nightmare.in multi thread programming at least i can make more connections for more average response time.

maybe i missed some point here,i have not much experience for backend development.however i still think using blocking io api is a suck idea while the framework is non-blocking.

Plan C: drop MySql use mongoDB, i heard mongoDB has an asynchronous Python implement api named asyncmongo, but im not sure the risk to learn mongoDB and take mongoDB as our business choice


Solution

  • Another option is to setup Multiprocessing ( http://docs.python.org/library/multiprocessing.html?highlight=multiprocessing#multiprocessing ) and use on of the processes as a Queue manager for the DB.

    Combine Multiprocessing with efficient SQL queries and you should minimize your overall impact. The blocking calls will only be done on the DB Processing Queue Manager process and the rest of the application can run without being blocked. The only people affected are the ones attempting to run a DB query at the same time.