小编Jas*_*son的帖子

Python运行一个守护进程子进程并读取stdout

我需要运行一个程序并将其输出收集到stdout.这个程序(socat)需要在python脚本的持续时间内在后台运行.一旦它运行,Socat就会处于dameon模式,但首先它会向stdout输出一些我需要用于其余脚本的行.

命令: socat -d -d PTY: PTY:

输出:

2011/03/23 21:12:35 socat[7476] N PTY is /dev/pts/1
2011/03/23 21:12:35 socat[7476] N PTY is /dev/pts/2
2011/03/23 21:12:35 socat[7476] N starting data transfer loop with FDs [3,3] and [5,5]
Run Code Online (Sandbox Code Playgroud)

...

我基本上想在我的程序开始时运行它并让它一直运行直到脚本终止,但我需要将两个/ dev/pts/X名称读入python.

谁能告诉我怎么做?

我想出了这个只是挂起,我猜是因为它阻止子进程终止.

#!/usr/bin/python
from subprocess import Popen, PIPE, STDOUT

cmd = 'socat -d -d PTY: PTY: &'

p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
output = p.stdout.read()

# Process the output 
print(output)
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助

编辑:似乎它可能写入stderr,但脚本仍然只是在有和没有从stderr读取甚至没有读取.

python

12
推荐指数
1
解决办法
1万
查看次数

标签 统计

python ×1