<tfoot id='NhrRi'></tfoot>

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

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

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

          <bdo id='NhrRi'></bdo><ul id='NhrRi'></ul>
      1. 当父母在python中崩溃时杀死子进程

        时间:2023-07-22
        <i id='XWJQJ'><tr id='XWJQJ'><dt id='XWJQJ'><q id='XWJQJ'><span id='XWJQJ'><b id='XWJQJ'><form id='XWJQJ'><ins id='XWJQJ'></ins><ul id='XWJQJ'></ul><sub id='XWJQJ'></sub></form><legend id='XWJQJ'></legend><bdo id='XWJQJ'><pre id='XWJQJ'><center id='XWJQJ'></center></pre></bdo></b><th id='XWJQJ'></th></span></q></dt></tr></i><div id='XWJQJ'><tfoot id='XWJQJ'></tfoot><dl id='XWJQJ'><fieldset id='XWJQJ'></fieldset></dl></div>
              <tbody id='XWJQJ'></tbody>

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

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

                  本文介绍了当父母在python中崩溃时杀死子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试编写一个 python 程序来测试用 C 编写的服务器.python 程序使用 subprocess 模块启动编译的服务器:

                  I am trying to write a python program to test a server written in C. The python program launches the compiled server using the subprocess module:

                  pid = subprocess.Popen(args.server_file_path).pid
                  

                  这工作正常,但是如果 python 程序由于错误而意外终止,则生成的进程将继续运行.我需要一种方法来确保如果 python 程序意外退出,服务器进程也会被终止.

                  This works fine, however if the python program terminates unexpectedly due to an error, the spawned process is left running. I need a way to ensure that if the python program exits unexpectedly, the server process is killed as well.

                  更多细节:

                  • 仅限 Linux 或 OSX 操作系统
                  • 不能以任何方式修改服务器代码

                  推荐答案

                  我会atexit.register 终止进程的函数:

                  I would atexit.register a function to terminate the process:

                  import atexit
                  process = subprocess.Popen(args.server_file_path)
                  atexit.register(process.terminate)
                  pid = process.pid
                  

                  或许:

                  import atexit
                  process = subprocess.Popen(args.server_file_path)
                  @atexit.register
                  def kill_process():
                      try:
                          process.terminate()
                      except OSError:
                          pass #ignore the error.  The OSError doesn't seem to be documented(?)
                               #as such, it *might* be better to process.poll() and check for 
                               #`None` (meaning the process is still running), but that 
                               #introduces a race condition.  I'm not sure which is better,
                               #hopefully someone that knows more about this than I do can 
                               #comment.
                  
                  pid = process.pid
                  

                  <小时>

                  请注意,如果你做了一些讨厌的事情导致 python 以非优雅的方式死亡(例如,通过 os._exit 或者如果你导致 SegmentationFaultBusError)


                  Note that this doesn't help you if you do something nasty to cause python to die in a non-graceful way (e.g. via os._exit or if you cause a SegmentationFault or BusError)

                  这篇关于当父母在python中崩溃时杀死子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:subprocess.wait() 不等待 Popen 进程完成(使用线程时)? 下一篇:对消息进行 grepping 时,bufsize 必须是整数错误

                  相关文章

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

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

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