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

<tfoot id='jhTBy'></tfoot>

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

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

        <i id='jhTBy'><tr id='jhTBy'><dt id='jhTBy'><q id='jhTBy'><span id='jhTBy'><b id='jhTBy'><form id='jhTBy'><ins id='jhTBy'></ins><ul id='jhTBy'></ul><sub id='jhTBy'></sub></form><legend id='jhTBy'></legend><bdo id='jhTBy'><pre id='jhTBy'><center id='jhTBy'></center></pre></bdo></b><th id='jhTBy'></th></span></q></dt></tr></i><div id='jhTBy'><tfoot id='jhTBy'></tfoot><dl id='jhTBy'><fieldset id='jhTBy'></fieldset></dl></div>
      1. 如何杀死多进程中的所有池工作人员?

        时间:2023-05-26

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

          <tbody id='rzm0r'></tbody>

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

              • <legend id='rzm0r'><style id='rzm0r'><dir id='rzm0r'><q id='rzm0r'></q></dir></style></legend>

                1. 本文介绍了如何杀死多进程中的所有池工作人员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我想停止单个工作人员的所有线程.

                  I want to stop all threads from a single worker.

                  我有一个有 10 个工人的线程池:

                  I have a thread pool with 10 workers:

                  def myfunction(i):
                      print(i) 
                      if (i == 20):
                          sys.exit()
                  
                  p = multiprocessing.Pool(10, init_worker) 
                  
                  for i in range(100):
                      p.apply_async(myfunction, (i,))
                  

                  我的程序不会停止,其他进程会继续工作,直到完成所有 100 次迭代.我想完全从调用 sys.exit() 的线程内部停止池.目前的写法只会停止调用sys.exit()的worker.

                  My program does not stop and the other processes continue working until all 100 iterations are complete. I want to stop the pool entirely from inside the thread that calls sys.exit(). The way it is currently written will only stop the worker that calls sys.exit().

                  推荐答案

                  这不是你想要的方式,因为在工作进程中调用 sys.exit() 只会终止工人.它对父进程或其他工作进程没有影响,因为它们是独立的进程,提高 SystemExit 只会影响当前进程.您需要向父进程发送一个信号,告诉它应该关闭.为您的用例执行此操作的一种方法是使用 Eventmultiprocessing.Manager 服务器:

                  This isn't working the way you're intending because calling sys.exit() in a worker process will only terminate the worker. It has no effect on the parent process or the other workers, because they're separate processes and raising SystemExit only affects the current process. You need to send a signal back the parent process to tell it that it should shut down. One way to do this for your use-case would be to use an Event created in a multiprocessing.Manager server:

                  import multiprocessing
                  
                  def myfunction(i, event):
                      if not event.is_set():
                          print i 
                      if i == 20:
                          event.set()
                  
                  if __name__ == "__main__":
                      p= multiprocessing.Pool(10) 
                      m = multiprocessing.Manager()
                      event = m.Event()
                      for i in range(100):
                          p.apply_async(myfunction , (i, event))
                      p.close()
                  
                      event.wait()  # We'll block here until a worker calls `event.set()`
                      p.terminate() # Terminate all processes in the Pool
                  

                  输出:

                  0
                  1
                  2
                  3
                  4
                  5
                  6
                  7
                  8
                  9
                  10
                  11
                  12
                  13
                  14
                  15
                  16
                  17
                  18
                  19
                  20
                  

                  正如 Luke 的回答中所指出的,这里有一场竞赛:不能保证所有工作人员都会按顺序运行,因此 myfunction(20, ..) 可能会在之前运行例如,myfuntion(19, ..).20 之后的其他工作人员也可能在主进程可以对正在设置的事件采取行动之前运行.我通过在打印 i 之前添加 if not event.is_set(): 调用来减小比赛窗口的大小,但它仍然存在.

                  As pointed out in Luke's answer, there is a race here: There's no guarantee that all the workers will run in order, so it's possible that myfunction(20, ..) will run prior to myfuntion(19, ..), for example. It's also possible that other workers after 20 will run before the main process can act on the event being set. I reduced the size of the race window by adding the if not event.is_set(): call prior to printing i, but it still exists.

                  这篇关于如何杀死多进程中的所有池工作人员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:python多处理的生产者/消费者问题 下一篇:如何在 Python 中使用 Managers() 在多个进程之间共享

                  相关文章

                  最新文章

                2. <tfoot id='S4E2e'></tfoot>

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

                  2. <legend id='S4E2e'><style id='S4E2e'><dir id='S4E2e'><q id='S4E2e'></q></dir></style></legend>

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