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
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()
Check out our new txMySQL project which can do this now.
This is a native asynchronous implementation of the binary MySQL protocol.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With