为什么 python.subprocess 在 proc.communicate() 之后挂起?

2022-01-18 00:00:00 python subprocess

问题描述

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

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

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

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
')

print "alive"
time.sleep(6)

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

程序现在可以很好地处理第一个输入S ",但随后停止,第二个L "被忽略了.

the program now goes well with the first input 'S ', but then stopped, and I the second 'L ' 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 读取.这意味着写入标准输入的数据不会进入程序.这通常适用于密码提示.
    • 请记住,您必须考虑 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
    L
    ')[0]
    print output
    
  • 相关文章