我想使用与postgres数据库的异步连接来插入实时的数据。我正在使用Twisted进行TCP通信,我正在为txpostgres提供与数据库交互的镜头。当我尝试为异步插入添加回调时,我堆叠了一个奇怪的消息。这是我的代码:
try:
conn = txpostgres.ConnectionPool(50,params)
d = conn.start()
def save_conn(c):
self.list_cnx.append(c)
print str(c),"Connect OK"
def print_err(m):
print m
d.addCallbacks(lambda _: save_conn(conn),lambda __: print_err('Connect NO'))
except Exception as e:
print "Cannot connect to database!!"
我在列表中添加了connction池的引用以供将来查询。
def insert_data(self,dic):
try:
insArRq="""INSERT INTO test_pool(date_msg, msg) VALUES ('%s','%s')"""%(dic['date'],dic['msg'])
for c in self.list_cnx:
def insert_finich():
print "insert finich"
def insert_error():
print "insert error"
d = c.runOperation(insArRq) # return a deferred as mentioned in the documentation
print d # for debug
d.addCallBack(insert_finich) # error mesage
except Exception as ee:
print "Insert error : ",ee
当我尝试为runOperation返回的延迟添加回调时,会出现此错误:
<Deferred at 0x8d9782c waiting on Deferred at 0x8d9786c>
Insert error : Deferred instance has no attribute 'addCallBack'
有时候:
<Deferred at 0x8d97a0c>
Insert error : Deferred instance has no attribute 'addCallBack'
请帮助我,我是一个新的解决概念,所以我想我错过了什么。谢谢
答案 0 :(得分:1)
该方法名为addCallback
;案件很重要。