我在Python服务器脚本中运行两个子进程。子流程的目的是从我的Raspberry Pi流式传输视频。如何用其他命令杀死子进程python
我的问题是如何杀死子进程时,另一个命令发送到服务器。我正在使用Popen()来启动子进程。
这是我的代码,当服务器收到命令“startStream”。我使用Twisted库作为服务器协议。
class Echo(Protocol):
def connectionMade(self):
#self.transport.write("""connected""")
self.factory.clients.append(self)
print "clients are ", self.factory.clients
def connectionLost(self, reason):
self.factory.clients.remove(self)
def dataReceived(self, data):
print "data is ", data
if data == "startStream":
p = subprocess.Popen("raspistill --nopreview -w 640 -h 480 -q 5 -o /tmp/stream/pic.jpg -tl 100 -t 9999999 -th 0:0:0 &", shell=True)
pn = subprocess.Popen("LD_LIBRARY_PATH=/usr/local/lib mjpg_streamer -i %s -o %s &" % (x,y), shell=True)
我想要的是这样的。
if data == "startStream":
p = subprocess.Popen("raspistill --nopreview -w 640 -h 480 -q 5 -o /tmp/stream/pic.jpg -tl 100 -t 9999999 -th 0:0:0 &", shell=True)
pn = subprocess.Popen("LD_LIBRARY_PATH=/usr/local/lib mjpg_streamer -i %s -o %s &" % (x,y), shell=True)
elif data == "stopStream":
os.kill(p.pid)
os.kill(pn.pid)
非常感谢!
'terminate()'? – BusyAnt
问题是当调用stopStream时,p和pn不可访问。我习惯于使用Java,在那里我可以刚刚声明过程为全局变量,然后从任何地方访问它们,但显然这在Python中不起作用。 – Oliver
此外,该模块没有任何属性终止 – Oliver