有没有人知道python的异步mysql库?

And*_*Kou 11 python mysql asynchronous twisted tornado

我一直在寻找用于python(龙卷风,扭曲等)的非阻塞服务器,但是如果没有与数据库的非阻塞连接,那么很多好处似乎都会丢失.有谁知道是否有任何项目可以解决这个问题?(通过非阻塞la node.js)

编辑:澄清我的问题

daf*_*daf 13

您可以使用Twisted的ADBAPI来包装同步DBAPI实现.

例如:

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()
Run Code Online (Sandbox Code Playgroud)


Ant*_* P. 1

这样做的方法是在单独的线程中生成数据库查询。对于 Twisted,您可以使用deferToThread()or deferToThreadPool()(请参阅 API 文档1)。

  • 此外,twisted.enterprise.adbapi 在“deferToThreadPool”之上提供了类似 DB-API 的 API,专门用于在基于 Twisted 的应用程序中更轻松地访问 MySQL 和其他 RDBMS。 (2认同)