• <small id='dr6qa'></small><noframes id='dr6qa'>

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

      <tfoot id='dr6qa'></tfoot>
        <bdo id='dr6qa'></bdo><ul id='dr6qa'></ul>
    2. <legend id='dr6qa'><style id='dr6qa'><dir id='dr6qa'><q id='dr6qa'></q></dir></style></legend>

        多处理模块中的 ThreadPool 与 Pool 有什么区别?

        时间:2023-05-26
          <tfoot id='PRq0x'></tfoot>

          • <legend id='PRq0x'><style id='PRq0x'><dir id='PRq0x'><q id='PRq0x'></q></dir></style></legend>
          • <small id='PRq0x'></small><noframes id='PRq0x'>

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

                  <bdo id='PRq0x'></bdo><ul id='PRq0x'></ul>
                  本文介绍了多处理模块中的 ThreadPool 与 Pool 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  multiprocessing 模块中的ThreadPoolPool 有什么区别.当我尝试我的代码时,这是我看到的主要区别:

                  Whats the difference between ThreadPool and Pool in multiprocessing module. When I try my code out, this is the main difference I see:

                  from multiprocessing import Pool
                  import os, time
                  
                  print("hi outside of main()")
                  
                  def hello(x):
                      print("inside hello()")
                      print("Proccess id: ", os.getpid())
                      time.sleep(3)
                      return x*x
                  
                  if __name__ == "__main__":
                      p = Pool(5)
                      pool_output = p.map(hello, range(3))
                  
                      print(pool_output)
                  

                  我看到以下输出:

                  hi outside of main()
                  hi outside of main()
                  hi outside of main()
                  hi outside of main()
                  hi outside of main()
                  hi outside of main()
                  inside hello()
                  Proccess id:  13268
                  inside hello()
                  Proccess id:  11104
                  inside hello()
                  Proccess id:  13064
                  [0, 1, 4]
                  

                  使用线程池":

                  from multiprocessing.pool import ThreadPool
                  import os, time
                  
                  print("hi outside of main()")
                  
                  def hello(x):
                      print("inside hello()")
                      print("Proccess id: ", os.getpid())
                      time.sleep(3)
                      return x*x
                  
                  if __name__ == "__main__":
                      p = ThreadPool(5)
                      pool_output = p.map(hello, range(3))
                  
                      print(pool_output)
                  

                  我看到以下输出:

                  hi outside of main()
                  inside hello()
                  inside hello()
                  Proccess id:  15204
                  Proccess id:  15204
                  inside hello()
                  Proccess id:  15204
                  [0, 1, 4]
                  

                  我的问题是:

                  • 为什么每次在Pool中都会运行outside __main__()"?

                  • why is the "outside __main__()" run each time in the Pool?

                  multiprocessing.pool.ThreadPool 不会产生新进程?它只是创建新线程?

                  multiprocessing.pool.ThreadPool doesn't spawn new processes? It just creates new threads?

                  如果是这样,使用 multiprocessing.pool.ThreadPool 与仅使用 threading 模块有什么区别?

                  If so whats the difference between using multiprocessing.pool.ThreadPool as opposed to just threading module?

                  我在任何地方都没有看到任何关于 ThreadPool 的官方文档,有人可以帮我看看在哪里可以找到它吗?

                  I don't see any official documentation for ThreadPool anywhere, can someone help me out where I can find it?

                  推荐答案

                  multiprocessing.pool.ThreadPool 的行为与 multiprocessing.Pool 相同,唯一的区别是使用线程而不是进程来运行工作者逻辑.

                  The multiprocessing.pool.ThreadPool behaves the same as the multiprocessing.Pool with the only difference that uses threads instead of processes to run the workers logic.

                  你看到的原因

                  hi outside of main()
                  

                  使用 multiprocessing.Pool 多次打印是因为池将 spawn 5 个独立进程.每个进程都会初始化自己的 Python 解释器并加载模块,从而导致顶层 print 再次执行.

                  being printed multiple times with the multiprocessing.Pool is due to the fact that the pool will spawn 5 independent processes. Each process will initialize its own Python interpreter and load the module resulting in the top level print being executed again.

                  请注意,只有在使用 spawn 进程创建方法时才会发生这种情况(仅适用于 Windows 的方法).如果您使用 fork 之一(Unix),您将看到消息只打印一次,就像线程一样.

                  Note that this happens only if the spawn process creation method is used (only method available on Windows). If you use the fork one (Unix), you will see the message printed only once as for the threads.

                  multiprocessing.pool.ThreadPool 没有记录,因为它的实现从未完成.它缺乏测试和文档.你可以在源代码中看到它的实现.

                  The multiprocessing.pool.ThreadPool is not documented as its implementation has never been completed. It lacks tests and documentation. You can see its implementation in the source code.

                  我相信下一个自然问题是:何时使用基于线程的池以及何时使用基于进程的池?

                  I believe the next natural question is: when to use a thread based pool and when to use a process based one?

                  经验法则是:

                  • IO 绑定作业 ->multiprocessing.pool.ThreadPool
                  • CPU 绑定作业 ->multiprocessing.Pool
                  • 混合工作 ->取决于工作量,由于进程隔离带来的优势,我通常更喜欢 multiprocessing.Pool

                  在 Python 3 上,您可能需要查看 concurrent.future.Executor 池实现.

                  On Python 3 you might want to take a look at the concurrent.future.Executor pool implementations.

                  这篇关于多处理模块中的 ThreadPool 与 Pool 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将 asyncio 与多处理结合起来会出现什么样的问题 下一篇:了解多处理:Python 中的共享内存管理、锁和队列

                  相关文章

                  最新文章

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

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

                      <tfoot id='NuIwp'></tfoot>