【问题标题】:Run rtorrent on background use Python在后台使用 Python 运行 rtorrent
【发布时间】:2011-09-23 16:36:58
【问题描述】:

我可以使用 Python 在后台运行 rtorrent 客户端吗? 我正在尝试使用 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]

这个脚本应该运行 rTorrent 并返回一个带有 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

此脚本正在尝试使用 rTorrent 运行线程并在循环中键入 PID。 但是,当我将客户端作为标准输入和标准输出的 /dev/null 运行时,它不会运行。当我更改为: process = Popen(self.torrent.getRun(),stdout=PIPE,stdin=PIPE) 在这段代码中,主线程正在等待 rtorrent 关闭。

也许有人会帮助解决这个问题,或者我做错了什么。

【问题讨论】:

  • 用Python写的deluge会不会更方便些?

标签: python background daemon


【解决方案1】:

这里有两个问题:

  1. process.communicate() 将阻塞直到进程终止。
  2. rtorrent 被设计为在终端中运行,如果你不在 tty/pty 中运行它是没有用的。

【讨论】:

  • 非常感谢。我明白我的错误。我不会运行 rtorrent 使用屏幕并从锁定文件中获取 PID。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-02
相关资源
最近更新 更多