2014-11-21 23 views
2

如果我在reactor事件循環中創建了Deferred並添加了一些回調函數,如果讓本地引用超出範圍,它是否會被調用?舉例來說,如果我有一個connectionMade()像這樣的協議:扭曲的Python - 如果推遲超出範圍,它會被解僱嗎?

def connectionMade(self): 
    # This made up function returns a deferred that will connect to 
    # some remote server using some made up protocol and return some data. 
    d = connectToRemoteServer(reactor, url) 
    d.addCallback(self._handleRemoteConnection) 
    # At this point, d is going to go out of scope, so will the deferred 
    # it points to ever get fired? 

def _handleRemoteConnection(self, data): 
    # Do something with the data 

我已經看到了使用雙絞線不同的代碼這種模式,和我無法理解爲什麼延期是從connectToRemoteServer()返回不 當d超出範圍時收集垃圾。我認爲它永遠不會開火,或者由於競賽狀況而導致隨機失敗。任何人都可以向我解釋爲什麼這有效嗎?我已閱讀http://twistedmatrix.com/documents/current/core/howto/defer.html幾次,但我仍然不確定爲什麼這會起作用?

感謝,

卡爾

+1

對象在沒有對它們的引用時被垃圾收集。因此,'d'不是對象的唯一引用,因爲它不是垃圾收集。添加一個'print(sys.getrefcount(d))'... – kindall 2014-11-21 00:17:18

回答

1

假想connectToRemoteServer API將要舉辦一個參考d內部,通常與來自全球reactor一個對象時會觸發d(通過調用d.callback)時的參考由d表示的操作完成。

因此,引用從堆棧到reactor.run(因爲反應堆正在運行)的堆棧幀到d