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

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

      1. <small id='z34eI'></small><noframes id='z34eI'>

          <bdo id='z34eI'></bdo><ul id='z34eI'></ul>
      2. 保持子进程活着并继续给它命令?Python

        时间:2023-07-22

              <small id='0UXEM'></small><noframes id='0UXEM'>

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

                    <tbody id='0UXEM'></tbody>
                • <tfoot id='0UXEM'></tfoot>
                  本文介绍了保持子进程活着并继续给它命令?Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果我使用给定命令在 python 中生成一个新的 subprocess(假设我使用 python 命令启动 python 解释器),我如何将新数据发送到过程(通过 STDIN)?

                  If I spawn a new subprocess in python with a given command (let's say I start the python interpreter with the python command), how can I send new data to the process (via STDIN)?

                  推荐答案

                  使用标准的subprocess模块.您使用 subprocess.Popen() 启动进程,它将在后台运行(即与您的 Python 程序同时运行).当您调用 Popen() 时,您可能希望将 stdin、stdout 和 stderr 参数设置为 subprocess.PIPE.然后就可以使用返回对象上的stdin、stdout和stderr字段来读写数据了.

                  Use the standard subprocess module. You use subprocess.Popen() to start the process, and it will run in the background (i.e. at the same time as your Python program). When you call Popen(), you probably want to set the stdin, stdout and stderr parameters to subprocess.PIPE. Then you can use the stdin, stdout and stderr fields on the returned object to write and read data.

                  未经测试的示例代码:

                  from subprocess import Popen, PIPE
                  
                  # Run "cat", which is a simple Linux program that prints it's input.
                  process = Popen(['/bin/cat'], stdin=PIPE, stdout=PIPE)
                  process.stdin.write(b'Hello
                  ')
                  process.stdin.flush()
                  print(repr(process.stdout.readline())) # Should print 'Hello
                  '
                  process.stdin.write(b'World
                  ')
                  process.stdin.flush()  
                  print(repr(process.stdout.readline())) # Should print 'World
                  '
                  
                  # "cat" will exit when you close stdin.  (Not all programs do this!)
                  process.stdin.close()
                  print('Waiting for cat to exit')
                  process.wait()
                  print('cat finished with return code %d' % process.returncode)
                  

                  这篇关于保持子进程活着并继续给它命令?Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python 作为 Windows 服务运行:OSError: [WinError 6] 句柄无效 下一篇:Python的子进程不允许使用shell = True进行进程替换?

                  相关文章

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

                    1. <small id='XC5AU'></small><noframes id='XC5AU'>

                      <tfoot id='XC5AU'></tfoot>

                    2. <legend id='XC5AU'><style id='XC5AU'><dir id='XC5AU'><q id='XC5AU'></q></dir></style></legend>
                        <bdo id='XC5AU'></bdo><ul id='XC5AU'></ul>