0
votes

Can I run the rtorrent client in the background using Python? I am trying to do it using PIPE import threading from subprocess import Popen, PIPE

class RunClient(threading.Thread):
    queue_cmd = None 
    torrent = None
    def __init__(self,q_cmd,torrent):
        self.queue_cmd = q_cmd
        threading.Thread.__init__(self)
        self.torrent = torrent

    def run(self):
        """Run client"""
        FNULL = open('/dev/null', 'w')
        print(self.torrent.getRun())
        process = Popen(self.torrent.getRun(),stdout=FNULL,stdin=FNULL)
        self.queue_cmd.put(process)
        process.communicate()[0]

This script should run rTorrent and return an object with its PID.

class Clients(threading.Thread):


    pids = {}
    q_cmd = None

    def __init__(self,q_cmd):
        """ """
        self.q_cmd = q_cmd
        threading.Thread.__init__(self)

    def startClient(self,id):
        """ """
        q = Queue.Queue(0)
        rClient = RunClient(q,Torrent())
        rClient.start()
        self.pids[id] = q.get()
        print self.pids

    def run(self):
        """Run torrent client"""
        print("Start thread...")
        self.startClient(50)
        i=0
        print("Start while...")
        while i<20:
            time.sleep(1)
            print(">>>",self.pids[50].pid)
            i=i+1

This script is trying to run a thread with rTorrent and in cycle type it PID. But when I run the client as /dev/null for stdin and stdout, it doesn't run. When I change to: process = Popen(self.torrent.getRun(),stdout=PIPE,stdin=PIPE) In this code, the main thread is waiting while rtorrent is closed.

Maybe someone will help resolve this issue or maybe I am doing something wrong.

1
Wouldn't it be easier to use deluge, which is written in Python? - Nemo

1 Answers

0
votes

Two problems here:

  1. process.communicate() will block until the process is terminated.
  2. rtorrent is designed to be run in a terminal, it is useless if you don't run it in a tty/pty.