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

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

    <tfoot id='f0oKW'></tfoot>
  • <legend id='f0oKW'><style id='f0oKW'><dir id='f0oKW'><q id='f0oKW'></q></dir></style></legend>

        如何在 Python 中生成一个新的独立进程

        时间:2023-07-21
        <legend id='9N2KL'><style id='9N2KL'><dir id='9N2KL'><q id='9N2KL'></q></dir></style></legend>
          <tbody id='9N2KL'></tbody>
            <bdo id='9N2KL'></bdo><ul id='9N2KL'></ul>
              <tfoot id='9N2KL'></tfoot>

              1. <small id='9N2KL'></small><noframes id='9N2KL'>

                <i id='9N2KL'><tr id='9N2KL'><dt id='9N2KL'><q id='9N2KL'><span id='9N2KL'><b id='9N2KL'><form id='9N2KL'><ins id='9N2KL'></ins><ul id='9N2KL'></ul><sub id='9N2KL'></sub></form><legend id='9N2KL'></legend><bdo id='9N2KL'><pre id='9N2KL'><center id='9N2KL'></center></pre></bdo></b><th id='9N2KL'></th></span></q></dt></tr></i><div id='9N2KL'><tfoot id='9N2KL'></tfoot><dl id='9N2KL'><fieldset id='9N2KL'></fieldset></dl></div>
                  本文介绍了如何在 Python 中生成一个新的独立进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一些 Python 代码偶尔需要跨越一个新进程以即发即弃"的方式运行 shell 脚本,即没有阻塞.shell 脚本不会与原始 Python 代码通信,实际上可能会终止调用 Python 进程,因此启动的 shell 脚本不能是调用 Python 进程的子进程.我需要它作为一个独立的进程启动.

                  I have a some Python code that occasionally needs to span a new process to run a shell script in a "fire and forget" manner, i.e. without blocking. The shell script will not communicate with the original Python code and will in fact probably terminate the calling Python process, so the launched shell script cannot be a child process of the calling Python process. I need it to be launched as an independent process.

                  换句话说,假设我有 mycode.py 并启动 script.sh.然后 mycode.py 将继续处理而不会阻塞.脚本 script.sh 将独立做一些事情,然后实际停止并重新启动 mycode.py.所以运行script.py的进程必须完全独立于mycode.py.我该怎么做?我认为 subprocess.Popen 不会阻塞,但仍会创建一个子进程,一旦 mycode.py 停止就会终止,这不是我想要的.

                  In other words, let's say I have mycode.py and that launches script.sh. Then mycode.py will continue processing without blocking. The script script.sh will do some things independently and will then actually stop and restart mycode.py. So the process that runs script.py must be completely independent of mycode.py. How exactly can I do this? I think subprocess.Popen will not block, but will still create a child process that terminates as soon as mycode.py stops, which is not what I want.

                  推荐答案

                  尝试在前面加上 "nohup" 到 script.sh.您可能需要决定如何处理 stdout 和 stderr;我只是把它放在示例中.

                  Try prepending "nohup" to script.sh. You'll probably need to decide what to do with stdout and stderr; I just drop it in the example.

                  import os
                  from subprocess import Popen
                  
                  devnull = open(os.devnull, 'wb') # Use this in Python < 3.3
                  # Python >= 3.3 has subprocess.DEVNULL
                  Popen(['nohup', 'script.sh'], stdout=devnull, stderr=devnull)
                  

                  这篇关于如何在 Python 中生成一个新的独立进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用 Python 运行外部可执行文件? 下一篇:在后台执行子进程

                  相关文章

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

                    <bdo id='GrxFP'></bdo><ul id='GrxFP'></ul>
                • <small id='GrxFP'></small><noframes id='GrxFP'>

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