Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does anyone know of a asynchronous mysql lib for python?

I've been looking into non-blocking servers for python (tornado, twisted etc) but a lot of the benefits seems to be lost if there's no non-blocking connection to the database. Does anyone know if there are any projects that take care of this? (by non-blocking a la node.js)

Edit: Clarified my question

like image 492
Andrew Kou Avatar asked Dec 13 '09 21:12

Andrew Kou


3 Answers

You can use Twisted's ADBAPI to wrap a synchronous DBAPI implementation.

E.g.:

from twisted.internet import reactor
from twisted.enterprise import adbapi

def result(rows):
    for row in rows:
        print row

    reactor.stop()

def fail(err):
    err.printDetailedTraceback()
    reactor.stop()

pool = adbapi.ConnectionPool('sqlite3', 'animals.db')
d = pool.runQuery('SELECT * FROM animals', ())
d.addCallbacks(result, fail)
reactor.run()
like image 109
daf Avatar answered Nov 07 '22 05:11

daf


Check out our new txMySQL project which can do this now.

This is a native asynchronous implementation of the binary MySQL protocol.

like image 27
Luke Marsden Avatar answered Nov 07 '22 06:11

Luke Marsden


The way you do that is by spawning database queries in a separate thread. With Twisted you can use deferToThread() or deferToThreadPool() (see the API docs1).

like image 41
Antoine P. Avatar answered Nov 07 '22 05:11

Antoine P.