1
我正試圖在扭曲中執行簡單的回調序列。我的想法是,我登錄到服務,並且當登錄成功時,我開始發送命令。這是我的代碼:具有獨立數據的回調鏈,與鏈無關
from twisted.internet import reactor, defer
class MyService:
def __init__ (self):
self.handle = None
def login(self):
def onConnect(handle):
self.handle = handle
return
df = defer.Deferred().addCallback(onConnect)
reactor.callLater(2, df.callback, "dummyhandle")
return df
def sendCommand(self, command):
def onResult(result):
print result
return result
if self.handle == None:
print "Not logged in"
return
else:
df = defer.Deferred().addCallback(onResult, result ="xxx")
return df
my = MyService()
my.login().addCallback(my.sendCommand, command = "format")
reactor.run()
運行的代碼生成以下堆棧跟蹤:
Unhandled error in Deferred: Traceback (most recent call last): File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py", line 1179, in mainLoop
self.runUntilCurrent() File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py", line 778, in runUntilCurrent
call.func(*call.args, **call.kw) File "/usr/lib/python2.6/dist-packages/twisted/internet/defer.py", line 280, in callback
self._startRunCallbacks(result) File "/usr/lib/python2.6/dist-packages/twisted/internet/defer.py", line 354, in _startRunCallbacks
self._runCallbacks()
--- <exception caught here> --- File "/usr/lib/python2.6/dist-packages/twisted/internet/defer.py", line 371, in _runCallbacks
self.result = callback(self.result, *args, **kw) exceptions.TypeError: sendCommand() got multiple values for keyword argument 'command'
這裏最重要的是,sendCommand不需要登錄的結果,但不同的數據。它需要登錄已完成(這就是爲什麼我需要它在回調鏈中),但它對登錄的結果不感興趣(只要它不是錯誤,無論如何將在錯誤鏈中處理) 。
我在這裏做錯了什麼?