Python Twisted adbapi.ConnectionPool不起任何作用

Python Twisted adbapi.ConnectionPool不起任何作用,python,twisted,Python,Twisted,我尝试使用以下代码: from twisted.enterprise import adbapi dbpool = adbapi.ConnectionPool( "MySQLdb", db='test_db', port='3306', user='tester',

我尝试使用以下代码:

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,也没有显示错误。数据连接正常。

您需要启动反应堆。adbapi中的a是异步的,就像twisted中的其他东西一样。调用ConnectionPool.runQuery时,您要求twisted在后台执行一些工作,完成后,在runQuery返回的延迟中向您提供该操作的结果

但对于twisted来说,要想做任何事情,您必须启动它的事件循环。在最简单的情况下,您可以:

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

你需要启动反应堆。adbapi中的a是异步的,就像twisted中的其他东西一样。调用ConnectionPool.runQuery时,您要求twisted在后台执行一些工作,完成后,在runQuery返回的延迟中向您提供该操作的结果

但对于twisted来说,要想做任何事情,您必须启动它的事件循环。在最简单的情况下,您可以:

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

你需要启动反应堆吗?你需要启动反应堆吗?我应该注意,你只需要做一个简短的脚本。在任何大的扭曲系统或应用中,反应堆应该已经运行,如果这是在一些代码的中间做其他事情。在你的程序中,reactor应该只运行一次。我应该注意,你只需要为一个简短的脚本运行一次。在任何大的扭曲系统或应用中,反应堆应该已经运行,如果这是在一些代码的中间做其他事情。在你的程序中,反应堆应该只运行一次。