Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:多进程工作者,跟踪已完成的任务(缺少完成)_Python_Multiprocessing - Fatal编程技术网

Python:多进程工作者,跟踪已完成的任务(缺少完成)

Python:多进程工作者,跟踪已完成的任务(缺少完成),python,multiprocessing,Python,Multiprocessing,默认的多处理池代码包括一个计数器,用于跟踪工作人员已完成的任务数: completed += 1 logging.debug('worker exiting after %d tasks' % completed) 但是从range(12)上升到range(20)apool.map会导致计数器中出现错误(这似乎与工人创建无关)。我也不太清楚是什么原因造成的 例如: import multiprocessing as mp def ret_x(x): return x def

默认的
多处理池
代码包括一个计数器,用于跟踪工作人员已完成的任务数:

    completed += 1
logging.debug('worker exiting after %d tasks' % completed)
但是从
range(12)
上升到
range(20)
a
pool.map
会导致计数器中出现错误(这似乎与工人创建无关)。我也不太清楚是什么原因造成的

例如:

import multiprocessing as mp

def ret_x(x): 
    return x
def inform():
    print('made a worker!')
pool  = mp.Pool(2, maxtasksperchild=2, initializer=inform)
res= pool.map(ret_x, range(8))
print(res)
将在以下情况下正常工作:

made a worker!
made a worker!
worker exiting after 2 tasks
worker exiting after 2 tasks
made a worker!
worker exiting after 2 tasks
made a worker!
worker exiting after 2 tasks
[0, 1, 2, 3, 4, 5, 6, 7]
但是将
范围
更改为
20
不会显示正在创建的任何其他工作人员或总共20个已完成的任务,即使已完成的范围在预期列表中返回

made a worker!
made a worker!
worker exiting after 2 tasks
worker exiting after 2 tasks
made a worker!
worker exiting after 2 tasks
made a worker!
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
worker exiting after 1 tasks

这是因为您没有在pool.map中明确定义“chunksize”:

map(func, iterable[, chunksize])
此方法将iterable分割为若干个块,以供使用 作为单独的任务提交到流程池。(近似)尺寸 可以通过将chunksize设置为正值来指定这些块的大小 整数

资料来源:

对于8个项目,考虑到len(pool)=2,chunksize将为1(divmod(8,2*4)),因此您可以看到(8/1)/2个worker=4个worker

workers = (len of items / chunksize) /  tasks per process
对于20个项目,考虑len(pool)=2,chunksize将为3(divmode(20,2*4)),因此您会看到类似(20/3)/2=3.3的值

对于40…chunksize=5,workers=(40/5)/5=4个workers

workers = (len of items / chunksize) /  tasks per process
如果需要,可以将chunksize设置为1

res = pool.map(ret_x, range(40), 1)
你会看到(20/1)/2=10个工人

python mppp.py
made a worker!
made a worker!
made a worker!
made a worker!
made a worker!
made a worker!
made a worker!
made a worker!
made a worker!
made a worker!
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
所以chunksize就像一个过程的单位工作量……或者类似的东西


如何计算chunksize:

我明白了-我正在查看worker运行循环,并假设func
try:result=(True,func(*args,**kwds))
正在运行映射的
函数
输入,但它实际上是任务的块大小生成器。这并不明显。