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

      <tfoot id='ejjnQ'></tfoot>

    1. <legend id='ejjnQ'><style id='ejjnQ'><dir id='ejjnQ'><q id='ejjnQ'></q></dir></style></legend>

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

        即使直接子进程已终止,Popen 也会等待子进程

        时间:2023-07-22
          • <bdo id='xRiVl'></bdo><ul id='xRiVl'></ul>
              <legend id='xRiVl'><style id='xRiVl'><dir id='xRiVl'><q id='xRiVl'></q></dir></style></legend>
              • <tfoot id='xRiVl'></tfoot>

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

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

                • 本文介绍了即使直接子进程已终止,Popen 也会等待子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 Windows 8/XP 上使用 Python 2.7.

                  I'm working with Python 2.7 on Windows 8/XP.

                  我有一个程序 A 使用以下代码运行另一个程序 B:

                  I have a program A that runs another program B using the following code:

                  p = Popen(["B"], stdout=PIPE, stderr=PIPE)
                  stdout, stderr = p.communicate()
                  return
                  

                  B 运行批处理脚本 C.C 是一个长时间运行的脚本,即使 C 尚未完成,我也希望 B 退出.我已经使用以下代码(在 B 中)完成了它:

                  B runs a batch script C. C is a long running script and I want B to exit even though C has not finished. I have done it using the following code (in B):

                  p = Popen(["C"])
                  return
                  

                  当我运行 B 时,它按预期工作.但是,当我运行 A 时,我希望它在 B 退出时退出.但是即使 B 已经退出,A 也会等到 C 退出.关于正在发生的事情以及可能的解决方案有什么想法吗?

                  When I run B, it works as expected. When I run A however, I expected it to exit when B exits. But A waits until C exits even though B has already exitted. Any ideas on what's happening and what possible solutions could be?

                  不幸的是,将 A 更改为 B 的明显解决方案是不可行的.

                  Unfortunately, the obvious solution of changing A to look like B is not an option.

                  以下是说明此问题的功能示例代码:https://www.dropbox.com/s/cbplwjpmydogvu2/popen.zip?dl=1

                  Here is a functional sample code to illustrate this issue: https://www.dropbox.com/s/cbplwjpmydogvu2/popen.zip?dl=1

                  非常感谢任何意见.

                  推荐答案

                  您可以为 C 子进程提供 start_new_session 模拟:

                  You could provide start_new_session analog for the C subprocess:

                  #!/usr/bin/env python
                  import os
                  import sys
                  import platform
                  from subprocess import Popen, PIPE
                  
                  # set system/version dependent "start_new_session" analogs
                  kwargs = {}
                  if platform.system() == 'Windows':
                      # from msdn [1]
                      CREATE_NEW_PROCESS_GROUP = 0x00000200  # note: could get it from subprocess
                      DETACHED_PROCESS = 0x00000008          # 0x8 | 0x200 == 0x208
                      kwargs.update(creationflags=DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP)  
                  elif sys.version_info < (3, 2):  # assume posix
                      kwargs.update(preexec_fn=os.setsid)
                  else:  # Python 3.2+ and Unix
                      kwargs.update(start_new_session=True)
                  
                  p = Popen(["C"], stdin=PIPE, stdout=PIPE, stderr=PIPE, **kwargs)
                  assert not p.poll()
                  

                  [1]: CreateProcess 的进程创建标志()

                  这篇关于即使直接子进程已终止,Popen 也会等待子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:从子进程中实时捕获标准输出 下一篇:如何为 popen 指定工作目录

                  相关文章

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

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

                      <tfoot id='V6EBH'></tfoot><legend id='V6EBH'><style id='V6EBH'><dir id='V6EBH'><q id='V6EBH'></q></dir></style></legend>

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