线程池类似于多处理池? [英] Threading pool similar to the multiprocessing Pool?
问题描述
Is there a Pool class for worker threads, similar to the multiprocessing module's Pool class?
例如,我喜欢并行化地图函数的简单方法
I like for example the easy way to parallelize a map function
def long_running_func(p):
c_func_no_gil(p)
p = multiprocessing.Pool(4)
xs = p.map(long_running_func, range(100))
但是我希望这样做而又不增加创建新进程的开销.
however I would like to do it without the overhead of creating new processes.
我知道GIL.但是,在我的用例中,该函数将是一个IO绑定的C函数,python包装器将在实际函数调用之前为其释放GIL.
I know about the GIL. However, in my usecase, the function will be an IO-bound C function for which the python wrapper will release the GIL before the actual function call.
我必须编写自己的线程池吗?
Do I have to write my own threading pool?
推荐答案
我刚刚发现在multiprocessing
模块中实际上是 一个基于线程的Pool接口,但是有些隐藏并且没有正确记录.
I just found out that there actually is a thread-based Pool interface in the multiprocessing
module, however it is hidden somewhat and not properly documented.
可以通过
from multiprocessing.pool import ThreadPool
它是使用包装Python线程的虚拟Process类实现的.可以在 multiprocessing.dummy
中找到此基于线程的Process类. 在文档一个>.该虚拟模块应该提供基于线程的整个多处理接口.
It is implemented using a dummy Process class wrapping a python thread. This thread-based Process class can be found in multiprocessing.dummy
which is mentioned briefly in the docs. This dummy module supposedly provides the whole multiprocessing interface based on threads.
这篇关于线程池类似于多处理池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!