Andrew Kou
Andrew Kou

Reputation: 7115

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

Upvotes: 11

Views: 4847

Answers (3)

Luke Marsden
Luke Marsden

Reputation: 131

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

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

Upvotes: 4

daf
daf

Reputation: 5240

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()

Upvotes: 13

Antoine P.
Antoine P.

Reputation: 4315

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).

Upvotes: 1

Related Questions