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

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

        子进程 Popen 和 call 有什么区别(我该如何使用它们)?

        What#39;s the difference between subprocess Popen and call (how can I use them)?(子进程 Popen 和 call 有什么区别(我该如何使用它们)?)
        <i id='YipU6'><tr id='YipU6'><dt id='YipU6'><q id='YipU6'><span id='YipU6'><b id='YipU6'><form id='YipU6'><ins id='YipU6'></ins><ul id='YipU6'></ul><sub id='YipU6'></sub></form><legend id='YipU6'></legend><bdo id='YipU6'><pre id='YipU6'><center id='YipU6'></center></pre></bdo></b><th id='YipU6'></th></span></q></dt></tr></i><div id='YipU6'><tfoot id='YipU6'></tfoot><dl id='YipU6'><fieldset id='YipU6'></fieldset></dl></div>
      1. <tfoot id='YipU6'></tfoot>

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

                    <tbody id='YipU6'></tbody>

                • <legend id='YipU6'><style id='YipU6'><dir id='YipU6'><q id='YipU6'></q></dir></style></legend>

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

                  本文介绍了子进程 Popen 和 call 有什么区别(我该如何使用它们)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想从 Python 调用一个外部程序.我已经使用了 Popen()call() 来做到这一点.

                  两者有什么区别?

                  我的具体目标是从 Python 运行以下命令.我不确定重定向是如何工作的.

                  ./my_script.sh >输出

                  我阅读了 文档,上面写着 call() 是一个便利功能或快捷功能.我们是否会因为使用 call() 而不是 Popen() 而失去任何权力?

                  解决方案

                  重定向有两种方式.两者都适用于 subprocess.Popensubprocess.call.

                  1. 设置关键字参数 shell = Trueexecutable =/path/to/the/shell 并指定命令,就像你在那里一样.

                  2. 由于您只是将输出重定向到文件,请设置关键字参数

                    stdout = an_open_writeable_file_object

                    对象指向输出文件的位置.

                  subprocess.Popen 更多一般比 subprocess.call.p>

                  Popen 不会阻塞,允许您在进程运行时与其进行交互,或继续在 Python 程序中进行其他操作.对 Popen 的调用返回一个 Popen 对象.

                  call 确实 阻塞.虽然它支持与 Popen 构造函数相同的所有参数,因此您仍然可以设置进程的输出、环境变量等,但您的脚本会等待程序完成,然后 call 返回一个代表进程退出状态的代码.

                  returncode = call(*args, **kwargs)

                  和调用基本一样

                  returncode = Popen(*args, **kwargs).wait()

                  call 只是一个便利功能.它在 CPython 中的实现位于 subprocess.py:

                  def call(*popenargs, timeout=None, **kwargs):"""带参数运行命令.等待命令完成或超时,然后返回 returncode 属性.参数与 Popen 构造函数的参数相同.例子:retcode = call(["ls", "-l"])"""使用 Popen(*popenargs, **kwargs) 作为 p:尝试:返回 p.wait(超时=超时)除了:p.kill()p.wait()增加

                  如您所见,它是对 Popen 的薄包装.

                  I want to call an external program from Python. I have used both Popen() and call() to do that.

                  What's the difference between the two?

                  My specific goal is to run the following command from Python. I am not sure how redirects work.

                  ./my_script.sh > output
                  

                  I read the documentation and it says that call() is a convenience function or a shortcut function. Do we lose any power by using call() instead of Popen()?

                  解决方案

                  There are two ways to do the redirect. Both apply to either subprocess.Popen or subprocess.call.

                  1. Set the keyword argument shell = True or executable = /path/to/the/shell and specify the command just as you have it there.

                  2. Since you're just redirecting the output to a file, set the keyword argument

                    stdout = an_open_writeable_file_object
                    

                    where the object points to the output file.

                  subprocess.Popen is more general than subprocess.call.

                  Popen doesn't block, allowing you to interact with the process while it's running, or continue with other things in your Python program. The call to Popen returns a Popen object.

                  call does block. While it supports all the same arguments as the Popen constructor, so you can still set the process' output, environmental variables, etc., your script waits for the program to complete, and call returns a code representing the process' exit status.

                  returncode = call(*args, **kwargs) 
                  

                  is basically the same as calling

                  returncode = Popen(*args, **kwargs).wait()
                  

                  call is just a convenience function. It's implementation in CPython is in subprocess.py:

                  def call(*popenargs, timeout=None, **kwargs):
                      """Run command with arguments.  Wait for command to complete or
                      timeout, then return the returncode attribute.
                  
                      The arguments are the same as for the Popen constructor.  Example:
                  
                      retcode = call(["ls", "-l"])
                      """
                      with Popen(*popenargs, **kwargs) as p:
                          try:
                              return p.wait(timeout=timeout)
                          except:
                              p.kill()
                              p.wait()
                              raise
                  

                  As you can see, it's a thin wrapper around Popen.

                  这篇关于子进程 Popen 和 call 有什么区别(我该如何使用它们)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 画布滚动条?)
                • <small id='Z0qqI'></small><noframes id='Z0qqI'>

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

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

                    1. <tfoot id='Z0qqI'></tfoot>
                        <bdo id='Z0qqI'></bdo><ul id='Z0qqI'></ul>

                              <tbody id='Z0qqI'></tbody>