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

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

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

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

        python如何以分离模式运行进程

        时间:2023-05-25
          <bdo id='kq2h9'></bdo><ul id='kq2h9'></ul>
                <tbody id='kq2h9'></tbody>
              <tfoot id='kq2h9'></tfoot>

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

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

                <legend id='kq2h9'><style id='kq2h9'><dir id='kq2h9'><q id='kq2h9'></q></dir></style></legend>
                  本文介绍了python如何以分离模式运行进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  这是一个例子:

                  from multiprocessing import Process
                  import time
                  
                  
                  def func():
                      print('sub process is running')
                      time.sleep(5)
                      print('sub process finished')
                  
                  
                  if __name__ == '__main__':
                      p = Process(target=func)
                      p.start()
                      print('done')
                  

                  我希望主进程在启动子进程后立即终止.但是在打印出完成"之后,终端仍在等待......有没有办法做到这一点,以便主进程在打印出完成"后立即退出,而不是等待子进程?我在这里很困惑,因为我没有调用 p.join()

                  what I expect is that the main process will terminate right after it start a subprocess. But after printing out 'done', the terminal is still waiting....Is there any way to do this so that the main process will exit right after printing out 'done', instead of waiting for subprocess? I'm confused here because I'm not calling p.join()

                  推荐答案

                  如果存在非守护进程.

                  通过在start()调用前设置daemon属性,可以使进程成为守护进程.

                  By setting, daemon attribute before start() call, you can make the process daemonic.

                  p = Process(target=func)
                  p.daemon = True  # <-----
                  p.start()
                  print('done')
                  

                  注意:不会打印sub process finished消息;因为主进程将在退出时终止子进程.这可能不是你想要的.

                  NOTE: There will be no sub process finished message printed; because the main process will terminate sub-process at exit. This may not be what you want.

                  你应该做双叉:

                  import os
                  import time
                  from multiprocessing import Process
                  
                  
                  def func():
                      if os.fork() != 0:  # <--
                          return          # <--
                      print('sub process is running')
                      time.sleep(5)
                      print('sub process finished')
                  
                  
                  if __name__ == '__main__':
                      p = Process(target=func)
                      p.start()
                      p.join()
                      print('done')
                  

                  这篇关于python如何以分离模式运行进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python 2.7.6 中多处理的奇怪 Queue.PriorityQueue 行为 下一篇:Python多处理简单的方法来实现一个简单的计数器

                  相关文章

                  最新文章

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

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

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