Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python-连续读取linux命令输出_Python_Python 3.x - Fatal编程技术网

python-连续读取linux命令输出

python-连续读取linux命令输出,python,python-3.x,Python,Python 3.x,我有一个命令,每隔几秒钟提供一次事件流-新消息。 我如何理解python附带的内容 标准方法 def getMessage(command): lines = os.popen(command).readlines() return lines 等待命令完成,但在此命令中将永远运行。它将继续并每隔几秒钟将新消息打印到stdout。 如何将其传递给python?我想捕获流中的所有消息。您可以逐行读取输出并处理/打印它。同时使用p.poll检查流程是否已结束 def getMess

我有一个命令,每隔几秒钟提供一次事件流-新消息。 我如何理解python附带的内容

标准方法

def getMessage(command):
    lines = os.popen(command).readlines()
    return lines
等待命令完成,但在此命令中将永远运行。它将继续并每隔几秒钟将新消息打印到stdout。
如何将其传递给python?我想捕获流中的所有消息。

您可以逐行读取输出并处理/打印它。同时使用
p.poll
检查流程是否已结束

def getMessage(command):
    full_message = ""
    p = subprocess.Popen(command, stdout=subprocess.PIPE)
    while True:
        output = p.stdout.readline()
        if output == '' and p.poll() is not None:
            break
        if output:
            fullmessage += output
            print(output.strip())
    return full_message

我需要做一些小的修改:
p=subprocess.Popen(command,stdout=subprocess.PIPE,shell=True,universal\u newlines=True)
,但除此之外它工作得很好,谢谢!