python - 为什么我无法从通过 Python 脚本调用的进程中获取输出?

标签 python linux process

基于此 Stackoverflow问题,我正在尝试通过 Python 获取进程的输出。

这里有代码:

from subprocess import Popen, PIPE

connection_string = "yowsup-cli demos --yowsup --config config"
popen_parameters = connection_string.split(" ")
proc = Popen(popen_parameters, stdout=PIPE, stderr=PIPE)

while True:
    line = proc.stdout.readline()

    if line != '':
        #the real code does filtering here
        print "Result:", line.rstrip()

    proc.stdout.flush()

不幸的是,没有任何东西被归还。顺便说一句,我需要与发送和接收数据的进程进行交互。我怎样才能完成这项工作?

谢谢!

最佳答案

while 循环没有条件退出,即使命令输出已发送。 试试这个:

if line != '':
    #the real code does filtering here
    print "Result:", line.rstrip()
else:
    break

关于python - 为什么我无法从通过 Python 脚本调用的进程中获取输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29183396/

相关文章:

python - 解析 JSON 时字符串索引必须是整数 - python

python - CPU和GPU平台的pytorch的requirements.txt

c++ - 当我对共享库使用 "stripped, with debug_info"命令时, "file"是什么意思?

python - python程序之间的通信

c# - 即使在 ProcessWindowStyle.Hidden 之后,控制台窗口仍然弹出;

python - django 静态文件版本控制

python - 如何修复 "E: Package ' libatlas-dev'没有安装候选者“在为 Scikit 学习安装 Scipy 时?

linux - 在 Linux 上使用静态链接的应用程序有什么缺点吗?

c - 如何在linux(32位)下编译C版vtd-xml

linux - 我如何更改 debian 中的优先级?