【发布时间】:2014-01-25 04:11:05
【问题描述】:
我对 Python 还是很陌生,但我掌握了基础知识。我正在尝试编写一个允许我与 Deluge 的 API 交互的脚本。现在我只是想找回当前的下载队列,但反应堆一直在运行。如果我将 reactor.stop() 放在 Deluge().onGetSessionState() 的末尾,那么反应器会在 Deluge().onGetTorrentStatus() 回来之前停止。
当 onGetSessionState 从 onGetTorrentStatus 获得所需的一切时,我对如何处理停止反应器感到困惑。
from deluge.ui.client import client
from twisted.internet import reactor
class Deluge(object):
def __init__(self,*args):
for key, value in enumerate(args):
self.key = value
def getDownloadQueue(self):
self.connect("getQueue")
def connect(self,params):
deluge = client.connect()
deluge.addCallback(self.onConnect,params).addErrback(self.onConnectFail).addBoth(self.disconnect)
reactor.run()
def disconnect(self):
client.disconnect()
reactor.stop()
def onConnect(self,result,params):
def onGetTorrentStatus(torrentInfo):
print torrentInfo["name"] + " " + torrentInfo["label"]
def onGetSessionState(torrent_ids):
# This prints the torrent_ids just fine
print torrent_ids
# This only works if I keep the self.disconnect() below commented out
for id in torrent_ids:
client.core.get_torrent_status(id, ["name","label"]).addCallback(onGetTorrentStatus)
if params == "getQueue":
client.core.get_session_state().addCallback(onGetSessionState)
# self.disconnect()
def onConnectFail(self,result):
print "Error: %s" % result
reactor.stop()
deluge = Deluge()
deluge.getDownloadQueue()
【问题讨论】: