简体   繁体   English

获取Python中所有正在运行的进程的STDOUT,STDERR

[英]Get STDOUT, STDERR of all running processes in Python

I'm a Python noob. 我是Python新手。

Is there a way to get the STDOUT and STDERR of all running processes in Python? 有没有办法在Python中获取所有正在运行的进程的STDOUT和STDERR? Some processes exit after a long time and their status is constantly being written to STDOUT. 一些进程在很长一段时间后退出,并且它们的状态不断地写入STDOUT。

To get subprocess output line by line without waiting for it to close: 要逐行获取子流程输出而不等待其关闭:

p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
while True:
    o = p.stdout.readline()
    if not o:
        break
    print o

you can use p.stdout.read(n) if want to read every 'n' characters from the process. 如果要从进程中读取每个“ n”个字符,则可以使用p.stdout.read(n)。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM