Python 多处理模块中的 ThreadPool 与 Pool 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46045956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between ThreadPool vs Pool in the multiprocessing module?
提问by ozn
Whats the difference between ThreadPool
and Pool
in multiprocessing
module. When I try my code out, this is the main difference I see:
ThreadPool
和Pool
在multiprocessing
模块中有什么区别。当我尝试我的代码时,这是我看到的主要区别:
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)
I see the following 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]
With "ThreadPool":
使用“线程池”:
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)
I see the following output:
我看到以下输出:
hi outside of main()
inside hello()
inside hello()
Proccess id: 15204
Proccess id: 15204
inside hello()
Proccess id: 15204
[0, 1, 4]
My questions are:
我的问题是:
why is the “outside __main__()” run each time in the
Pool
?multiprocessing.pool.ThreadPool
doesn't spawn new processes? It just creates new threads?If so whats the difference between using
multiprocessing.pool.ThreadPool
as opposed to justthreading
module?
为什么每次都在 __main__() 之外运行
Pool
?multiprocessing.pool.ThreadPool
不会产生新的进程?它只是创建新线程?如果是这样,使用
multiprocessing.pool.ThreadPool
与仅使用threading
模块有什么区别?
I don't see any official documentation for ThreadPool
anywhere, can someone help me out where I can find it?
我在任何ThreadPool
地方都没有看到任何官方文档,有人可以帮我找到它吗?
回答by noxdafox
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.
该multiprocessing.pool.ThreadPool
行为一样multiprocessing.Pool
,唯一的区别在于使用线程,而不是进程运行的工人逻辑。
The reason you see
你看到的原因
hi outside of main()
being printed multiple times with the multiprocessing.Pool
is due to the fact that the pool will spawn5 independent processes. Each process will initialize its own Python interpreter and load the module resulting in the top level print
being executed again.
被多次打印multiprocessing.Pool
是因为池将产生5 个独立进程。每个进程将初始化自己的 Python 解释器并加载模块,从而print
再次执行顶层。
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.
请注意,只有在使用spawn
进程创建方法时才会发生这种情况(仅在 Windows 上可用的方法)。如果您使用fork
一个(Unix),您将看到仅打印一次线程的消息。
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.
将multiprocessing.pool.ThreadPool
作为其实现从未完成未记录。它缺乏测试和文档。您可以在源代码中看到它的实现。
I believe the next natural question is: when to use a thread based pool and when to use a process based one?
我相信下一个自然的问题是:何时使用基于线程的池以及何时使用基于进程的池?
The rule of thumb is:
经验法则是:
- IO bound jobs ->
multiprocessing.pool.ThreadPool
- CPU bound jobs ->
multiprocessing.Pool
- Hybrid jobs -> depends on the workload, I usually prefer the
multiprocessing.Pool
due to the advantage process isolation brings
- IO 绑定作业 ->
multiprocessing.pool.ThreadPool
- CPU 绑定作业 ->
multiprocessing.Pool
- 混合作业 -> 取决于工作量,
multiprocessing.Pool
由于进程隔离带来的优势,我通常更喜欢
On Python 3 you might want to take a look at the concurrent.future.Executor
pool implementations.
在 Python 3 上,您可能需要查看concurrent.future.Executor
池实现。