为什么python.subprocess 在proc.communicate() 之后挂起? [英] why does python.subprocess hang after proc.communicate()?

查看:36
本文介绍了为什么python.subprocess 在proc.communicate() 之后挂起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 my_own_exe 的交互式程序.首先,它打印出alive,然后你输入S\n,然后它再次打印出alive.最后输入L\n.它做了一些处理并退出.

I've got an interactive program called my_own_exe. First, it prints out alive, then you input S\n and then it prints out alive again. Finally you input L\n. It does some processing and exits.

但是,当我从以下 python 脚本调用它时,该程序似乎在打印出第一个 'alive' 后挂起.

However, when I call it from the following python script, the program seemed to hang after printing out the first 'alive'.

这里有人能告诉我为什么会这样吗?

Can anyone here tell me why this is happening?

//阅读完后续(谢谢大家),我修改了代码如下:

// after reading the follow ups (thank you guys), i modified the code as following:

import subprocess
import time

base_command = "./AO_FelixStrategy_UnitTest --bats 31441 --chix 12467 --enxutp 31884 --turq 26372 --symbol SOGN --target_date " + '2009-Oct-16'
print base_command

proc2 = subprocess.Popen(base_command, shell=True , stdin=subprocess.PIPE,)

time.sleep(2);
print "aliv"
proc2.communicate('S\n')

print "alive"
time.sleep(6)

print "alive"
print proc2.communicate('L\n')
time.sleep(6)

该程序现在与第一个输入 'S\n' 运行良好,但随后停止,而第二个 'L\n' 有点被忽略.

the program now goes well with the first input 'S\n', but then stopped, and I the second 'L\n' is kinda ignored.

谁能告诉我为什么会这样?

Can anyone give me an idea why it's like this?

推荐答案

来自 communicate 的文档:

From the docs for communicate:

与进程交互:将数据发送到标准输入.从 stdout 和 stderr 读取数据,直到到达文件结尾.等待进程终止.

Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate.

因此在 communicate() 运行后,该进程已终止.

So after communicate() runs, the process has been terminated.

如果你想在不等待进程停止的情况下写入和读取:

If you want to write and read without waiting for the process to stop:

  • 不要永远使用 shell=True - 它不需要调用一个 shell 来调用你的程序,所以你和你之间会有另一个进程你的程序.这有很多令人不快的副作用.默认是 shell=False 所以你应该坚持下去.将您的 Popen 行更改为:

  • Don't ever use shell=True - it needlessy invokes a shell to in turn call your program, so there will be another process between you and your program. That has lots of unpleasant side-effects. The default is shell=False so you should stick with that. Change your Popen line to:

p = subprocess.Popen(["./AO_FelixStrategy_UnitTest",
                      "--bats", "31441", "--chix", "12467",
                      "--enxutp", "31884", "--turq", "26372",
                      "--symbol", "SOGN", "--target_date", '2009-Oct-16'],
                     stdin=subprocess.PIPE, 
                     stdout=subprocess.PIPE)

  • 使用 p.stdin.write 写入进程.使用 p.stdout.read 从中读取.

  • Use p.stdin.write to write to the process. Use p.stdout.read to read from it.

    • 它可能是从其他地方读取,而不是标准输入.有些程序直接从终端读取,有些程序使用一些 OS API 来读取.这意味着写入 stdin 的数据不会进入程序.对于密码提示,这通常是正确的.
    • 请记住,您必须考虑 AO_FelixStrategy_UnitTest 缓冲区.默认情况下,标准 C PIPE 通信是缓冲的,因此在关闭输入端之前您可能看不到任何输出(通过执行 p.stdin.close().除非 AO_FelixStrategy_UnitTest> 定期刷新输出.
    • It could be reading from somewhere else, not standard input. Some programs read directly from the terminal, others use some OS API to read. That means data written to stdin won't go to the program. This is often true for password prompts.
    • Remember that you have to account for AO_FelixStrategy_UnitTest buffers. By default standard C PIPE communication is buffered so you may not see any output until after you've closed the input side (by doing p.stdin.close(). Unless AO_FelixStrategy_UnitTest flushes the output periodically.

    这是一些示例代码,基于您的描述.它的工作方式取决于 AO_FelixStrategy_UnitTest 的开发方式:

    Here's some example code, based on what you describe. It could work depending on how AO_FelixStrategy_UnitTest was developed:

    p = subprocess.Popen(["./AO_FelixStrategy_UnitTest",
                          "--bats", "31441", "--chix", "12467",
                          "--enxutp", "31884", "--turq", "26372",
                          "--symbol", "SOGN", "--target_date", '2009-Oct-16'],
                         stdin=subprocess.PIPE, 
                         stdout=subprocess.PIPE)
    output = p.communicate('S\nL\n')[0]
    print output
    

    这篇关于为什么python.subprocess 在proc.communicate() 之后挂起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆