Page 1 of 1

Script not terminating

Posted: Fri Mar 07, 2014 2:42 am
by the4tress
I'm trying to make a script that will manage some of the tasks I have to deal with daily. Right now I just want it to list all of the torrents and their labels.

I can't figure out how to get the script to exit when it is finished. It doesn't look like twisted is disconnecting the reactor.

Code: Select all

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

    def getDownloadQueue(self):
        self.connect("getQueue")
        logging.debug("Finished getDownloadQueue()")
#       self.disconnect()

    def connect(self,params):
        logging.debug("client.connect()")
        deluge = client.connect()
        logging.debug("deluge.addCallback()")
        #deluge.addCallback(self.onConnect,params).addErrback(self.onConnectFail).addBoth(self.disconnect)
        from twisted.internet import task
        task.react(self.onConnect,[])
#       reactor.run()
        logging.debug("Finished connect()")

    def disconnect(self):
        client.disconnect()
#       reactor.stop()
        logging.debug("Finished disconnect()")

    def onConnect(self,result):
        params = "getQueue"
        def onGetTorrentStatus(torrentInfo):
            logging.debug("Starting onGetTorrentStatus()")
            #print torrentInfo["name"] + " " + torrentInfo["label"]
        #   if torrent["name"] == torrent_name:
        #       print "File '%s' already exists" % torrent["name"]
        #   return

        def onGetSessionState(torrent_ids):
            print torrent_ids

            logging.debug("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])

        if params == "getQueue":
            client.core.get_session_state().addCallback(onGetSessionState)
        logging.info("Finished onConnect")

    def onConnectFail(self,result):
        print "Error: %s" % result
#       reactor.stop()
I commented out some lines just for testing, but they don't seem to help.

I have also posted the question on stackoverflow, which got me a little further, but wasn't able to get that last piece working. Jean-Paul from stackoverflow suggested I use task.react(), but I couldn't find any valuable information on the method.

Re: Script not terminating

Posted: Sat Mar 08, 2014 2:22 am
by the4tress
Ok, I got a little further. Now it looks like I'm getting an error form the Deluge API.

Here is my code:

Code: Select all

#!/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()"

        # I addded this and commented out the line below for testing
        test2 = client.core.get_session_state()
        print test2
        #client.core.get_session_state().addCallback(self.onGetSessionState)
        print "Finished onConnect()"

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

Deluge().getDownloadQueue()
This is the output:

Code: Select all

Started getDownloadQueue()
Started connect()
task.react()
Started onConnect()
Traceback (most recent call last):
  File "./delugeTest.py", line 67, in <module>
    Deluge().getDownloadQueue()
  File "./delugeTest.py", line 18, in getDownloadQueue
    self.connect()
  File "./delugeTest.py", line 28, in connect
    test = task.react(self.onConnect, [])
  File "/usr/local/lib/python2.7/dist-packages/twisted/internet/task.py", line 867, in react
    finished = main(_reactor, *argv)
  File "./delugeTest.py", line 59, in onConnect
    test2 = client.core.get_session_state()
  File "/usr/lib/python2.7/dist-packages/deluge/ui/client.py", line 504, in __call__
    return self.daemon.call(self.base, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/deluge/ui/client.py", line 308, in call
    self.protocol.send_request(request)
AttributeError: 'NoneType' object has no attribute 'send_request'
Jean-Paul from Twisted said it looked like a Deluge error, not a Twisted error (stackoverflow post).

Thanks for any help!

Re: Script not terminating

Posted: Sat Mar 08, 2014 9:53 am
by Cas
I can't actually run your script as I don't have twisted >=12.3 installed here...

Re: Script not terminating

Posted: Sun Mar 09, 2014 2:26 am
by the4tress
Any suggestions where I could get more help? I've been trying to figure this out on my own for about a month and I'm not sure where to look for help.

Thanks for any information!

Re: Script not terminating

Posted: Sun Mar 09, 2014 5:15 pm
by Cas
I suggest you drop the use of task and go back to basics, see my answer on SO and the example here: http://dev.deluge-torrent.org/wiki/Deve ... 3/UIClient

Re: Script not terminating

Posted: Sun Mar 09, 2014 5:48 pm
by the4tress
OK. I guess I was getting ahead of myself.

Thanks!