如何停止扭曲回调?

How to stop Twisted callback?

我使用 twisted 写了一个回调函数现在我的问题是当 websocket 连接断开时我需要停止这个回调。

def sendBack(self, payload):
    # find correct way to identify connection dropped now using wasnotcl...
    if self.wasNotCleanReason:
        # stop this callback
    self.sendMessage("Message")
    reactor.callLater(delay, self.sendBack, payload=payload) 

你可以cancel它...

callID = reactor.callLater(delay, self.sendBack, payload=payload)
    if self.wasNotCleanReason:
        callID.cancel()

<a href="http://twistedmatrix.com/documents/10.1.0/core/howto/time.html" rel="nofollow">ref</a>