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

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

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

      1. 读取/写入 Popen() 子进程

        时间:2023-07-21
          <tbody id='z1UAJ'></tbody>

          <legend id='z1UAJ'><style id='z1UAJ'><dir id='z1UAJ'><q id='z1UAJ'></q></dir></style></legend>
          • <bdo id='z1UAJ'></bdo><ul id='z1UAJ'></ul>
              <tfoot id='z1UAJ'></tfoot>

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

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

                  本文介绍了读取/写入 Popen() 子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 python subprocess.Popen() 调用与子进程交谈.在我的真实代码中,我正在实现一种 IPC,所以我想写入一些数据、读取响应、写入更多数据、读取响应等等.因此,我不能使用 Popen.communicate(),否则它适用于简单的情况.

                  I'm trying to talk to a child process using the python subprocess.Popen() call. In my real code, I'm implementing a type of IPC, so I want to write some data, read the response, write some more data, read the response, and so on. Because of this, I cannot use Popen.communicate(), which otherwise works well for the simple case.

                  这段代码显示了我的问题.它甚至从来没有得到第一个响应,挂在第一个阅读结果"上.为什么?我怎样才能使这项工作如我所愿?

                  This code shows my problem. It never even gets the first response, hangs at the first "Reading result". Why? How can I make this work as I expect?

                  import subprocess
                  p = subprocess.Popen(["sed", 's/a/x/g'],
                                       stdout = subprocess.PIPE,
                                       stdin = subprocess.PIPE)
                  
                  p.stdin.write("abc
                  ")
                  print "Reading result:"
                  print p.stdout.readline()
                  
                  p.stdin.write("cat
                  ")
                  print "Reading result:"
                  print p.stdout.readline()
                  

                  推荐答案

                  如果可以的话,我会尝试使用 Popen().communicate(),因为它为你做了很多好事,但是如果您需要完全按照您的描述使用 Popen(),则需要使用 -l 选项将 sed 设置为在换行符之后刷新其缓冲区:

                  I would try to use Popen().communicate() if you can as it does a lot of nice things for you, but if you need to use Popen() exactly as you described, you'll need to set sed to flush its buffer after newlines with the -l option:

                  p = subprocess.Popen(['sed', '-l', 's/a/x/g'],
                                       stdout=subprocess.PIPE,
                                       stdin=subprocess.PIPE)
                  

                  你的代码应该可以正常工作

                  and your code should work fine

                  这篇关于读取/写入 Popen() 子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 subprocess 模块是否会释放 python GIL? 下一篇:使子进程 find git 在 Windows 上可执行

                  相关文章

                    <bdo id='0hGuE'></bdo><ul id='0hGuE'></ul>
                • <small id='0hGuE'></small><noframes id='0hGuE'>

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