• <legend id='S9D09'><style id='S9D09'><dir id='S9D09'><q id='S9D09'></q></dir></style></legend>
    1. <small id='S9D09'></small><noframes id='S9D09'>

      <tfoot id='S9D09'></tfoot>
        <bdo id='S9D09'></bdo><ul id='S9D09'></ul>

        <i id='S9D09'><tr id='S9D09'><dt id='S9D09'><q id='S9D09'><span id='S9D09'><b id='S9D09'><form id='S9D09'><ins id='S9D09'></ins><ul id='S9D09'></ul><sub id='S9D09'></sub></form><legend id='S9D09'></legend><bdo id='S9D09'><pre id='S9D09'><center id='S9D09'></center></pre></bdo></b><th id='S9D09'></th></span></q></dt></tr></i><div id='S9D09'><tfoot id='S9D09'></tfoot><dl id='S9D09'><fieldset id='S9D09'></fieldset></dl></div>
      1. 为什么 python.subprocess 在 proc.communicate() 之后挂起?

        why does python.subprocess hang after proc.communicate()?(为什么 python.subprocess 在 proc.communicate() 之后挂起?)
          <tbody id='yd1jj'></tbody>
      2. <legend id='yd1jj'><style id='yd1jj'><dir id='yd1jj'><q id='yd1jj'></q></dir></style></legend>
      3. <tfoot id='yd1jj'></tfoot>

        <small id='yd1jj'></small><noframes id='yd1jj'>

          <bdo id='yd1jj'></bdo><ul id='yd1jj'></ul>
                1. <i id='yd1jj'><tr id='yd1jj'><dt id='yd1jj'><q id='yd1jj'><span id='yd1jj'><b id='yd1jj'><form id='yd1jj'><ins id='yd1jj'></ins><ul id='yd1jj'></ul><sub id='yd1jj'></sub></form><legend id='yd1jj'></legend><bdo id='yd1jj'><pre id='yd1jj'><center id='yd1jj'></center></pre></bdo></b><th id='yd1jj'></th></span></q></dt></tr></i><div id='yd1jj'><tfoot id='yd1jj'></tfoot><dl id='yd1jj'><fieldset id='yd1jj'></fieldset></dl></div>

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

                  问题描述

                  我有一个名为 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)
                  

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

                4. 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
                  

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!
                5. 相关文档推荐

                  Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                  map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                  Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                  Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                  Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                  tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)
                  <tfoot id='RTn0i'></tfoot>
                  <legend id='RTn0i'><style id='RTn0i'><dir id='RTn0i'><q id='RTn0i'></q></dir></style></legend>
                    <i id='RTn0i'><tr id='RTn0i'><dt id='RTn0i'><q id='RTn0i'><span id='RTn0i'><b id='RTn0i'><form id='RTn0i'><ins id='RTn0i'></ins><ul id='RTn0i'></ul><sub id='RTn0i'></sub></form><legend id='RTn0i'></legend><bdo id='RTn0i'><pre id='RTn0i'><center id='RTn0i'></center></pre></bdo></b><th id='RTn0i'></th></span></q></dt></tr></i><div id='RTn0i'><tfoot id='RTn0i'></tfoot><dl id='RTn0i'><fieldset id='RTn0i'></fieldset></dl></div>
                      <tbody id='RTn0i'></tbody>

                    <small id='RTn0i'></small><noframes id='RTn0i'>

                        <bdo id='RTn0i'></bdo><ul id='RTn0i'></ul>