2014-10-27 42 views
0

我想使用異步連接到postgres數據庫來插入實時包含數據。我正在使用Twisted進行TCP通信,並且我正在爲與數據庫的交互提供txpostgres鏡頭。當我嘗試爲我的異步插入添加回調時,我堆疊了一條奇怪的消息。以下代碼:txpostgres:延遲實例沒有屬性'addCallBack'

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!!" 

我在列表中添加了連接池的引用以供將來查詢。

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' 

請幫助我,我'新以破壞概念,所以我認爲我錯過了什麼。謝謝

回答

1

該方法被命名爲addCallback;案件很重要。

+0

PEBCAK.THanks雕文。 – Khairy 2014-10-27 10:36:19

相關問題