【问题标题】:Does anyone know of a asynchronous mysql lib for python?有人知道python的异步mysql库吗?
【发布时间】:2010-12-26 05:59:43
【问题描述】:

我一直在研究 Python 的非阻塞服务器(tornado、twisted 等),但如果没有与数据库的非阻塞连接,很多好处似乎都会丢失。有谁知道是否有任何项目可以解决这个问题? (通过非阻塞 la node.js)

编辑:澄清我的问题

【问题讨论】:

    标签: python mysql twisted asynchronous tornado


    【解决方案1】:

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

    【讨论】:

    • 此外,twisted.enterprise.adbapi 在deferToThreadPool 之上提供了一个类似于 DB-API 的 API,专门用于在基于 Twisted 的应用程序中更轻松地访问 MySQL 和其他 rdbms。
    【解决方案2】:

    您可以使用 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()
    

    【讨论】:

      【解决方案3】:

      查看我们的新txMySQL project,它现在可以做到这一点。

      这是二进制 MySQL 协议的原生异步实现。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-03
        • 2023-03-08
        • 2011-03-31
        • 1970-01-01
        • 2012-05-22
        • 1970-01-01
        相关资源
        最近更新 更多