扭曲竞争条件

2024-05-18 06:33:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我的脚本中有一个种族条件的问题。我们的目标是连接到洪水并使用Twisted收集信息。在

脚本如下:

#!/usr/bin/python

import json
import sys
import os.path
from datetime import datetime

from deluge.ui.client import client
from twisted.internet import reactor, task

class Deluge(object):
    def __init__(self,*args):
        for key, value in enumerate(args):
            self.key = value

    def getDownloadQueue(self):
        print "Started getDownloadQueue()"
        self.connect()
        print "Finished getDownloadQueue()"

    def connect(self):
        print "Started connect()"
        deluge = client.connect()

        #deluge.addCallback(self.onConnect,params).addErrback(self.onConnectFail).addBoth(self.disconnect)

        print "task.react()"
        test = task.react(self.onConnect, [])
        print "deluge.addCallback()"
        test.addCallback(deluge).addErrback(self.onConnectFail).addBoth(self.disconnect)
        #deluge.addErrback(self.onConnectFail)
        print "Finished connect()"

    def disconnect(self):
        client.disconnect()
        print "Finished disconnect()"

    def onConnect(self, reactor):
        print "Started onConnect()"
        def onGetTorrentStatus(torrentInfo):
            print "Started onGetTorrentStatus()"
            print torrentInfo["name"] + " " + torrentInfo["label"]
            if torrent["name"] == torrent_name:
                print "File '%s' already exists" % torrent["name"]
            print "Finished onGetTorrentStatus()"
            return

        def onGetSessionState(torrent_ids):
            print "Started onGetSessionState()"
            print torrent_ids

            print "Got all torrent ids"
            for id in torrent_ids:
                d = client.core.get_torrent_status(id, ["name","label"]).addCallback(onGetTorrentStatus)
            print defer.gatherResults([d, self.disconnect])
            print "Finished onGetSessionState()"

        client.core.get_session_state().addCallback(self.onGetSessionState)
        print "Finished onConnect()"

    def onConnectFail(self,result):
        print "Error: %s" % result

Deluge().getDownloadQueue()

以下是它输出的错误:

^{pr2}$

这是我几个月前问的一个问题:How to properly stop Twisted reactor when callback is finished


Tags: nameimportselfclientdefconnecttorrentprint