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

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

      1. <tfoot id='Kt9ze'></tfoot>

        为什么 Python 多处理守护进程不打印到标准输出

        时间:2023-05-25
          <tbody id='CvBGh'></tbody>
        • <tfoot id='CvBGh'></tfoot>
              <bdo id='CvBGh'></bdo><ul id='CvBGh'></ul>
            • <legend id='CvBGh'><style id='CvBGh'><dir id='CvBGh'><q id='CvBGh'></q></dir></style></legend>
              <i id='CvBGh'><tr id='CvBGh'><dt id='CvBGh'><q id='CvBGh'><span id='CvBGh'><b id='CvBGh'><form id='CvBGh'><ins id='CvBGh'></ins><ul id='CvBGh'></ul><sub id='CvBGh'></sub></form><legend id='CvBGh'></legend><bdo id='CvBGh'><pre id='CvBGh'><center id='CvBGh'></center></pre></bdo></b><th id='CvBGh'></th></span></q></dt></tr></i><div id='CvBGh'><tfoot id='CvBGh'></tfoot><dl id='CvBGh'><fieldset id='CvBGh'></fieldset></dl></div>

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

                1. 本文介绍了为什么 Python 多处理守护进程不打印到标准输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我一直在尝试多处理,但遇到了守护进程.

                  I have been experimenting with multiprocessing, and running into a mindblock with daemons.

                  我有一个守护进程和一个非守护进程,守护进程无限期地每隔一秒发出一次输出,而非守护进程在启动时立即打印输出,休眠 3 秒,然后再次打印并返回.

                  I have one daemon and one non-daemon process, the daemon emitting output every one second indefinitely, while the non-daemon prints output immediately upon start, sleeps for 3 seconds, then prints again and returns.

                  问题是,守护进程的预期输出根本没有出现.

                  The problem is, the expected output from the daemon process doesn't show up at all.

                  回顾过去关于守护进程的 SO 问题,常见问题似乎是在守护进程之前结束的其他进程,或者需要刷新以显示输出的标准输出.两者都已解决(我认为),但我仍然只看到非守护进程的打印输出.

                  Reviewing past SO questions on daemons, the common issues appear to be either the other processes ending before the daemon, or the stdout requiring flushing to show output. Both have (I think) been addressed, however I continue to only see printed output from non-daemonic processes.

                  代码:

                  from multiprocessing import Process, current_process
                  import sys
                  import time
                  
                  def worker():
                      """
                      Announce that the process has started, sleep 3 seconds
                      then announce that the process is ending.
                      """
                      name = current_process().name
                      print name, 'starting...'
                      sys.stdout.flush()
                  
                      time.sleep(3)
                      print name, 'ending...'
                      sys.stdout.flush()
                  
                      return
                  
                  
                  def daemon():
                      """
                      Announce that the process has started, beep, then beep
                      once every second
                      """
                      name = current_process().name
                      print name, 'starting...'
                      print 'beep...'
                      sys.stdout.flush()
                  
                      while True:
                          time.sleep(1)
                          print 'beep...'
                          sys.stdout.flush()
                  
                  
                  if __name__=='__main__':
                      d = Process(target=daemon)
                      d.daemon = True
                      d.start()
                  
                      p = Process(target=worker)
                      p.daemon = False
                      p.start()
                  

                  预期输出:

                  Process-1 starting... # Order here may vary
                  beep...
                  Process-2 starting...
                  beep...
                  beep...
                  Process-2 ending... #There may or may not be another beep here
                  

                  实际生产出来的东西:

                  Process-2 starting...
                  Process-2 ending...
                  

                  任何关于为什么会发生这种情况的建议将不胜感激.

                  Any advice on why this is happening would be truly appreciated.

                  推荐答案

                  通过放置开启日志,可以更清晰的了解事件的顺序

                  You can get a clearer picture of the order of events by turning on logging by placing

                  import multiprocessing as mp
                  logger = mp.log_to_stderr(logging.INFO)
                  

                  在其他导入语句之后.然后你的程序会产生类似的东西:

                  after the other import statements. Then your program will yield something like:

                  [INFO/Process-1] child process calling self.run()
                  [INFO/MainProcess] process shutting down
                  Process-1 starting...
                  beep...
                  [INFO/Process-2] child process calling self.run()
                  [INFO/MainProcess] calling terminate() for daemon Process-1
                  Process-2 starting...
                  [INFO/MainProcess] calling join() for process Process-2
                  Process-2 ending...
                  [INFO/Process-2] process shutting down
                  [INFO/Process-2] process exiting with exitcode 0
                  [INFO/MainProcess] calling join() for process Process-1
                  

                  因此,主进程首先开始关闭,然后终止进程-1,即守护进程.这就是为什么您在 Process-2 继续时看不到任何哔哔声.

                  Thus, the main starts shutting down first, then it terminates Process-1, the daemon process. That's why you do not see any more beeps while Process-2 continues.

                  这篇关于为什么 Python 多处理守护进程不打印到标准输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何避免这种酸洗错误,在 Python 中并行化此代码 下一篇:使用多处理解析非常大的 XML 文件

                  相关文章

                  最新文章

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

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