扭曲的adbapi.ConnectionPool不做任何事情

时间:2012-12-27 20:28:26

标签: python twisted

我尝试使用以下代码:

from twisted.enterprise import adbapi   

    dbpool = adbapi.ConnectionPool(
                        "MySQLdb",
                        db='test_db',
                        port='3306',
                        user='tester',
                        passwd='some_pass',
                        host='localhost',
                        cp_reconnect=True
                    )

    dbpool.runQuery("INSERT INTO `htp_test` VALUES(NULL, 25, 'test')")

但是数据没有插入mysql,也没有显示错误。要连接的数据很好。

1 个答案:

答案 0 :(得分:4)

你需要启动反应堆。 adbapi中的“a”用于“异步”,就像扭曲的其他所有内容一样。当您致电ConnectionPool.runQuery()时,您已要求在后台执行某些工作,完成后,请在runQuery返回的延期中提供该操作的结果。

但是为了扭曲“做”任何事情,你有义务开始它的事件循环。在最简单的情况下,您可以:

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

def got_result(value):
    # do something, value won't be interesting on insert statements, though
    print "Horray"    
    # since this is all we want to do, stop the reactor
    reactor.stop()

d = dbpool.runQuery("INSERT INTO `htp_test` VALUES(NULL, 25, 'test')")
d.addCallback(got_result)

reactor.run()