0

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
    May 22, 2021 at 10:50

1 Answer 1

0

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.
1
  • thanks a lot. I understand my mistake. No wI run rtorrent use screen and get PID from lock file.
    – Eugene
    Jun 27, 2011 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.