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

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

      <tfoot id='NS4gT'></tfoot>

      Python:如何防止子进程接收 CTRL-C/Control-C/SIGINT

      时间:2023-07-21

    2. <small id='TnJBF'></small><noframes id='TnJBF'>

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

      <tfoot id='TnJBF'></tfoot>
      <legend id='TnJBF'><style id='TnJBF'><dir id='TnJBF'><q id='TnJBF'></q></dir></style></legend>
          <tbody id='TnJBF'></tbody>

              • <bdo id='TnJBF'></bdo><ul id='TnJBF'></ul>
              • 本文介绍了Python:如何防止子进程接收 CTRL-C/Control-C/SIGINT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我目前正在为在 shell 中运行的专用服务器开发一个包装器.包装器通过子进程生成服务器进程并观察其输出并对其做出反应.

                I am currently working on a wrapper for a dedicated server running in the shell. The wrapper spawns the server process via subprocess and observes and reacts to its output.

                必须明确地给专用服务器一个命令才能正常关闭.因此,CTRL-C 不能到达服务器进程.

                The dedicated server must be explicitly given a command to shut down gracefully. Thus, CTRL-C must not reach the server process.

                如果我在 python 中捕获 KeyboardInterrupt 异常或覆盖 SIGINT 处理程序,服务器进程仍会收到 CTRL-C 并立即停止.

                If I capture the KeyboardInterrupt exception or overwrite the SIGINT-handler in python, the server process still receives the CTRL-C and stops immediately.

                所以我的问题是:如何防止子进程接收到 CTRL-C/Control-C/SIGINT?

                So my question is: How to prevent subprocesses from receiving CTRL-C / Control-C / SIGINT?

                推荐答案

                #python IRC-Channel (Freenode) 有人帮我指出了 subprocess.Popen 的 preexec_fn 参数(...):

                Somebody in the #python IRC-Channel (Freenode) helped me by pointing out the preexec_fn parameter of subprocess.Popen(...):

                如果 preexec_fn 设置为可调用对象,这个对象将被调用子进程就在孩子被处决.(仅限 Unix)

                If preexec_fn is set to a callable object, this object will be called in the child process just before the child is executed. (Unix only)

                因此,以下代码解决了这个问题(仅限 UNIX):

                Thus, the following code solves the problem (UNIX only):

                import subprocess
                import signal
                
                def preexec_function():
                    # Ignore the SIGINT signal by setting the handler to the standard
                    # signal handler SIG_IGN.
                    signal.signal(signal.SIGINT, signal.SIG_IGN)
                
                my_process = subprocess.Popen(
                    ["my_executable"],
                    preexec_fn = preexec_function
                )
                

                注意:信号实际上并没有被阻止到达子进程.相反,上面的 preexec_fn 会覆盖信号的默认处理程序,从而忽略信号.因此,如果子进程再次覆盖 SIGINT 处理程序,此解决方案可能不起作用.

                Note: The signal is actually not prevented from reaching the subprocess. Instead, the preexec_fn above overwrites the signal's default handler so that the signal is ignored. Thus, this solution may not work if the subprocess overwrites the SIGINT handler again.

                另一个注意事项:此解决方案适用于各种子流程,即它也不限于用 Python 编写的子流程.例如,我正在为其编写包装器的专用服务器实际上是用 Java 编写的.

                Another note: This solution works for all sorts of subprocesses, i.e. it is not restricted to subprocesses written in Python, too. For example the dedicated server I am writing my wrapper for is in fact written in Java.

                这篇关于Python:如何防止子进程接收 CTRL-C/Control-C/SIGINT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Python中子进程读取线超时 下一篇:Python 子进程 Popen.communicate() 等价于 Popen.stdout.read()?

                相关文章

                    <tfoot id='IY5qL'></tfoot>
                      <bdo id='IY5qL'></bdo><ul id='IY5qL'></ul>
                  1. <small id='IY5qL'></small><noframes id='IY5qL'>

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