<legend id='EzBqo'><style id='EzBqo'><dir id='EzBqo'><q id='EzBqo'></q></dir></style></legend>
  • <small id='EzBqo'></small><noframes id='EzBqo'>

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

    <tfoot id='EzBqo'></tfoot>
      • <bdo id='EzBqo'></bdo><ul id='EzBqo'></ul>

        确保子进程在退出 Python 程序时死亡

        Ensuring subprocesses are dead on exiting Python program(确保子进程在退出 Python 程序时死亡)
        <i id='EZlsj'><tr id='EZlsj'><dt id='EZlsj'><q id='EZlsj'><span id='EZlsj'><b id='EZlsj'><form id='EZlsj'><ins id='EZlsj'></ins><ul id='EZlsj'></ul><sub id='EZlsj'></sub></form><legend id='EZlsj'></legend><bdo id='EZlsj'><pre id='EZlsj'><center id='EZlsj'></center></pre></bdo></b><th id='EZlsj'></th></span></q></dt></tr></i><div id='EZlsj'><tfoot id='EZlsj'></tfoot><dl id='EZlsj'><fieldset id='EZlsj'></fieldset></dl></div>

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

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

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

                    <tbody id='EZlsj'></tbody>
                  本文介绍了确保子进程在退出 Python 程序时死亡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有没有办法确保所有创建的子进程在 Python 程序退出时都死了?我所说的子进程是指那些使用 subprocess.Popen() 创建的.

                  Is there a way to ensure all created subprocess are dead at exit time of a Python program? By subprocess I mean those created with subprocess.Popen().

                  如果不是,我应该遍历所有发出的杀戮然后杀掉 -9 吗?有什么清洁的吗?

                  If not, should I iterate over all of the issuing kills and then kills -9? anything cleaner?

                  推荐答案

                  你可以使用 atexit 为此,并注册任何清理任务以在程序退出时运行.

                  You can use atexit for this, and register any clean up tasks to be run when your program exits.

                  atexit.register(func[, *args[, **kargs]])

                  在您的清理过程中,您还可以实现自己的等待,并在您希望的超时发生时将其终止.

                  In your cleanup process, you can also implement your own wait, and kill it when a your desired timeout occurs.

                  >>> import atexit
                  >>> import sys
                  >>> import time
                  >>> 
                  >>> 
                  >>>
                  >>> def cleanup():
                  ...     timeout_sec = 5
                  ...     for p in all_processes: # list of your processes
                  ...         p_sec = 0
                  ...         for second in range(timeout_sec):
                  ...             if p.poll() == None:
                  ...                 time.sleep(1)
                  ...                 p_sec += 1
                  ...         if p_sec >= timeout_sec:
                  ...             p.kill() # supported from python 2.6
                  ...     print 'cleaned up!'
                  ...
                  >>>
                  >>> atexit.register(cleanup)
                  >>>
                  >>> sys.exit()
                  cleaned up!
                  

                  注意 -- 如果该进程(父进程)被杀死,注册的函数将不会运行.

                  Note -- Registered functions won't be run if this process (parent process) is killed.

                  python >= 2.6 不再需要以下windows方法

                  这是一种在 Windows 中终止进程的方法.您的 Popen 对象具有 pid 属性,因此您可以通过 success = win_kill(p.pid) 调用它(需要 pywin32 已安装):

                  Here's a way to kill a process in windows. Your Popen object has a pid attribute, so you can just call it by success = win_kill(p.pid) (Needs pywin32 installed):

                      def win_kill(pid):
                          '''kill a process by specified PID in windows'''
                          import win32api
                          import win32con
                  
                          hProc = None
                          try:
                              hProc = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, pid)
                              win32api.TerminateProcess(hProc, 0)
                          except Exception:
                              return False
                          finally:
                              if hProc != None:
                                  hProc.Close()
                  
                          return True
                  

                  这篇关于确保子进程在退出 Python 程序时死亡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 画布滚动条?)
                  <legend id='RixWp'><style id='RixWp'><dir id='RixWp'><q id='RixWp'></q></dir></style></legend>
                    • <i id='RixWp'><tr id='RixWp'><dt id='RixWp'><q id='RixWp'><span id='RixWp'><b id='RixWp'><form id='RixWp'><ins id='RixWp'></ins><ul id='RixWp'></ul><sub id='RixWp'></sub></form><legend id='RixWp'></legend><bdo id='RixWp'><pre id='RixWp'><center id='RixWp'></center></pre></bdo></b><th id='RixWp'></th></span></q></dt></tr></i><div id='RixWp'><tfoot id='RixWp'></tfoot><dl id='RixWp'><fieldset id='RixWp'></fieldset></dl></div>
                        <tfoot id='RixWp'></tfoot>
                          <tbody id='RixWp'></tbody>

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

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