I want to use an asynchronous connection to a postgres database for inserting a realtime incomming data. I am using Twisted for the TCP communication and i am giving txpostgres a shot for the interaction with the database.I am stacked with a weird message when i try to add a callback for my asynchronous insert.Here my code:
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!!"
I am adding the refrence of the connction pool in a list for future query.
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
When i try to add a callback for the deferred returned by the runOperation this error appear:
<Deferred at 0x8d9782c waiting on Deferred at 0x8d9786c>
Insert error : Deferred instance has no attribute 'addCallBack'
and sometimes:
<Deferred at 0x8d97a0c>
Insert error : Deferred instance has no attribute 'addCallBack'
Help me please, i'am new to defrred concepts so i think i'am missing something.Thanks
The method is named addCallback
; case is important.