• <legend id='7W4Fl'><style id='7W4Fl'><dir id='7W4Fl'><q id='7W4Fl'></q></dir></style></legend>

          <bdo id='7W4Fl'></bdo><ul id='7W4Fl'></ul>
      1. <tfoot id='7W4Fl'></tfoot>

        <small id='7W4Fl'></small><noframes id='7W4Fl'>

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

        Windows上的python多处理

        时间:2023-05-25
        <legend id='axGKp'><style id='axGKp'><dir id='axGKp'><q id='axGKp'></q></dir></style></legend>
        • <bdo id='axGKp'></bdo><ul id='axGKp'></ul>

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

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

                • 本文介绍了Windows上的python多处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我是 python 编程的新手,需要一些帮助来理解 python 解释器流程,尤其是在多处理的情况下.请注意,我在 Windows 10 上运行 python 3.7.1.这是我的简单实验代码和输出.

                  I'm fairly new to python programming and need some help understanding the python interpreter flow, especially in the case of multiprocessing. Please note that I'm running python 3.7.1 on Windows 10. Here is my simple experimental code and the output.

                  import multiprocessing
                  import time
                  
                  
                  def calc_square(numbers, q):
                      for n in numbers:
                          q.put(n*n)
                          time.sleep(0.2)
                  
                      q.put(-1)
                      print('Exiting function')
                  
                  
                  print('Now in the main code. Process name is: ' + __name__)
                  
                  if __name__ == '__main__':
                      numbers = [2, 3, 4, 5]
                      q = multiprocessing.Queue()
                      p = multiprocessing.Process(target=calc_square, args=(numbers, q))
                  
                      p.start()
                  
                      while True:
                          if q.empty() is False:
                              nq = q.get()
                              print('Message is:' + str(nq))
                              if nq == -1:
                                  break
                      print('Done')
                  
                      p.join()
                  
                  Program Output:
                  
                  Now in the main code. Process name is: __main__
                  Now in the main code. Process name is: __mp_main__
                  Message is:4
                  Message is:9
                  Message is:16
                  Message is:25
                  Exiting function
                  Message is:-1
                  Done
                  
                  Process finished with exit code 0
                  

                  我见过不包含 if name 限定符的代码示例.我首先尝试运行其中一个并遇到几个错误,这些错误表明试图在进程初始化完成之前尝试启动进程.根据该代码的作者,它可以在 linux 上工作,因为它分叉,而 Windows 则不能(我后来了解到 Windows 使用 spawn 作为进程启动方法.).

                  I've seen code examples that do not contain the if name qualifier. I first tried running one of those and got several errors that pointed to trying to trying to start a process before process init had completed. According to the author of that code, it works on linux because it forks, while Windows does not (I learned later that Windows uses spawn as the process start method.).

                  我的问题集中在为什么进程似乎执行目标之外的指令.我很惊讶地看到在目标向父级发送消息之前打印的输出的第二行.为什么子进程解释器运行目标之外的代码?它真的在目标代码之前运行该代码吗?为什么?

                  My question centers around why the process seems to execute instructions that are outside of the target. I was surprised to see the 2nd line of the output which printed before the target sent a message to the parent. Why does the sub-process interpreter run that code that is outside of the target? Is it really running that code before the target code? Why?

                  推荐答案

                  在 Windows 上,新进程导入执行它的主脚本.这就是为什么在 Windows 上,您需要 if __name__ == '__main__': ...它会阻止 if 下的代码在每个进程中运行.

                  On Windows, the new processes import the main script, which executes it. This is why, on Windows, you need if __name__ == '__main__': ... it prevents code under that if from running in every process.

                  您的 Now in main code. 行位于 if 之外,因此它将在每个进程中运行.把它移到 if:

                  Your Now in main code. line is outside the if, so it will run in every process. Move it inside the if:

                  import multiprocessing
                  import time
                  
                  def calc_square(numbers, q):
                      for n in numbers:
                          q.put(n*n)
                          time.sleep(0.2)
                  
                      q.put(-1)
                      print('Exiting function')
                  
                  if __name__ == '__main__':
                      print('Now in the main code. Process name is:', __name__)
                      numbers = [2, 3, 4, 5]
                      q = multiprocessing.Queue()
                      p = multiprocessing.Process(target=calc_square, args=(numbers, q))
                      p.start()
                  
                      while True:
                          nq = q.get()
                          print('Message is:', nq)
                          if nq == -1:
                              break
                  
                      print('Done')
                      p.join()
                  

                  这篇关于Windows上的python多处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:交互式 Python 中的多处理包 下一篇:Python多处理帮助在条件下退出

                  相关文章

                  最新文章

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

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

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

                      <tfoot id='rGkMK'></tfoot>