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

    • <bdo id='DBpB0'></bdo><ul id='DBpB0'></ul>

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

    1. <legend id='DBpB0'><style id='DBpB0'><dir id='DBpB0'><q id='DBpB0'></q></dir></style></legend>
    2. <tfoot id='DBpB0'></tfoot>

        python子进程交互,为什么我的进程使用Popen.communicate,而不使用Popen.stdout.read

        Python subprocess interaction, why does my process work with Popen.communicate, but not Popen.stdout.read()?(python子进程交互,为什么我的进程使用Popen.communicate,而不使用Popen.stdout.read()?)

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

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

                • <bdo id='Paom4'></bdo><ul id='Paom4'></ul>

                  <legend id='Paom4'><style id='Paom4'><dir id='Paom4'><q id='Paom4'></q></dir></style></legend>
                  本文介绍了python子进程交互,为什么我的进程使用Popen.communicate,而不使用Popen.stdout.read()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 subprocess 模块与使用 Python 的命令行聊天机器人进行通信.(http://howie.sourceforge.net/使用编译好的win32二进制,我有我的理由!)

                  I am trying to communicate with a command-line chat bot with Python using the subprocess module. (http://howie.sourceforge.net/ using the compiled win32 binary, I have my reasons!)

                  这行得通:

                  proc = Popen('Howie/howie.exe', stdout=PIPE,stderr=STDOUT,stdin=PIPE)
                  output = proc.communicate()
                  

                  Popen.communicate 等待进程终止(并将其发送 EOF?),我希望能够与之交互.明显的解决方案是像这样读取 stdout/写入 stdin:

                  But Popen.communicate waits for the process to terminate (and sends it EOF?), I want to be able to interact with it. The apparent solution for this was to read stdout / write stdin like so:

                  这不起作用:

                  proc = Popen('Howie/howie.exe', stdout=PIPE,stderr=STDOUT,stdin=PIPE)
                  while True: print proc.stdout.readline()
                  

                  (请注意,我实际上使用的是基于 http://code.activestate.com 的更复杂的代码/recipes/440554/ 但问题是一样的.)

                  (Note that I am actually using more complex code based on http://code.activestate.com/recipes/440554/ but the issue is the same.)

                  问题是,第二种方法非常适合与 cmd 通信,但是当我运行聊天机器人时,什么也没有.所以我的问题是,捕获输出与使用 Popen.communicate() 有何不同?

                  The problem is, the second approach works perfectly for communicating to cmd, but when I run the chatbot, nothing. So my question is, how is this different in capturing output to using Popen.communicate()?

                  即我可以使用第二种方法照常使用命令行,直到我运行聊天机器人,此时我停止接收输出.使用第一种方法可以正确显示机器人的前几行输出,但我无法与之交互.

                  i.e. I can use the second approach to use the command line as per normal, until I run the chatbot, at which point I stop receiving output. Using the first approach correctly displays the first few lines of output from the bot, but leaves me unable to interact with it.

                  推荐答案

                  两者的一个主要区别是communicate()在发送数据后关闭stdin.我不知道你的具体情况,但在很多情况下,这意味着如果一个进程正在等待用户输入的结束,他会在使用communicate() 时得到它,而当代码阻塞时永远不会得到它read() 或 readline().

                  One major difference between the two is that communicate() closes stdin after sending the data. I don't know about your particular case, but in many cases this means that if a process is awaiting the end of the user input, he will get it when communicate() is used, and will never get it when the code blocks on read() or readline().

                  先尝试添加 Popen.stdin.close() 看看它是否会影响您的情况.

                  Try adding Popen.stdin.close() first and see if it affects your case.

                  这篇关于python子进程交互,为什么我的进程使用Popen.communicate,而不使用Popen.stdout.read()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                  • <bdo id='lTe8W'></bdo><ul id='lTe8W'></ul>

                    <legend id='lTe8W'><style id='lTe8W'><dir id='lTe8W'><q id='lTe8W'></q></dir></style></legend>

                  • <small id='lTe8W'></small><noframes id='lTe8W'>

                    • <tfoot id='lTe8W'></tfoot>

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