在 Python 中启动新流程而不是重复使用之前的流程
Initiate new processes instead of reusing prior in Python
我在 Python 中使用多处理:
import multiprocessing as mp
all_arguments = range(0,20)
pool = mp.Pool(processes=7)
all_items = [pool.apply_async(main_multiprocessing_function, args=(argument_value,)) for argument_value in all_arguments]
for item in all_items:
item.get()
在上面,据我所知,在工作处理器完成后,它会移动到下一个值。有什么方法可以强制 'new' 工作处理器每次都从头开始初始化,而不是重新使用旧处理器?
[具体来说,main_multiprocessing_function
调用多个其他函数,每个函数都使用缓存来加速每个任务中的处理。然而,所有这些缓存对于下一个要处理的项目来说都是多余的,因此我对一种将所有内容重置为新鲜状态的方法很感兴趣]。
来自docs:
maxtasksperchild is the number of tasks a worker process can complete
before it will exit and be replaced with a fresh worker process, to
enable unused resources to be freed. The default maxtasksperchild is
None, which means worker processes will live as long as the pool.
只需将池创建为
pool = mp.Pool(processes=7, maxtasksperchild=1)
我在 Python 中使用多处理:
import multiprocessing as mp
all_arguments = range(0,20)
pool = mp.Pool(processes=7)
all_items = [pool.apply_async(main_multiprocessing_function, args=(argument_value,)) for argument_value in all_arguments]
for item in all_items:
item.get()
在上面,据我所知,在工作处理器完成后,它会移动到下一个值。有什么方法可以强制 'new' 工作处理器每次都从头开始初始化,而不是重新使用旧处理器?
[具体来说,main_multiprocessing_function
调用多个其他函数,每个函数都使用缓存来加速每个任务中的处理。然而,所有这些缓存对于下一个要处理的项目来说都是多余的,因此我对一种将所有内容重置为新鲜状态的方法很感兴趣]。
来自docs:
maxtasksperchild is the number of tasks a worker process can complete before it will exit and be replaced with a fresh worker process, to enable unused resources to be freed. The default maxtasksperchild is None, which means worker processes will live as long as the pool.
只需将池创建为
pool = mp.Pool(processes=7, maxtasksperchild=1)