【发布时间】: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