2

私は以下のコードを使用しようとします:

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に挿入されず、エラーも表示されません。データの接続は良好です。

4

1 に答える 1

4

リアクターを起動する必要があります。"a" の "a"adbapiは "asynchronous" を意味し、twisted の他のほとんどすべてと同様です。を呼び出すとConnectionPool.runQuery()、twisted にバックグラウンドで何らかの作業を行うように依頼したことになります。それが完了すると、そのアクションの結果が によって返される deferred に返され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()
于 2012-12-27T20:44:50.297 に答える