[英]What does multiprocessing.pool actually do?
我的问题:我的代码现在是使用multiprocessing.pool做它应该做的吗?
def create_empty_file(file_name):
#for file in file_name:
# print(file)
#create a file named based on "file_name" argument if it doesnt exist in library
file = open(file_name, "x")
#delay 30 seconds
time.sleep(30)
#close the program
file.close()
#nodes should retrieve the next file to create as soon as one is done
#this will takes in argument as the file name
if __name__ == "__main__":
#so takes in the argument (names of files)
files = sys.argv[1:-1]
#number of processes
n_process = int(sys.argv[-1])
#create pool
pool = Pool(processes=n_process)
pool.map(create_empty_file, files)
我的代码完成了它应该做的事情,但我不确定我的代码是否真的按照我要求的方式工作。
Pool.map
是为所有工作进程绘制的共享多处理队列提供的。 每个工作程序运行无限(或固定迭代,如果设置maxtasksperchild
)循环:
它不会尝试在旧任务完成之前完成新任务,因此如果一个工作人员获得廉价任务,它将会执行更多任务; 只要任务仍然可用,任何工人都不会闲置。 所以,是的,它正在做你想要的; 它不是一些循环或其他静态工作分配方案,可能会使工作人员闲置而其他人则因多个任务等待而过载。 设置chunksize
可以创建这种效果,但在这种情况下,更多的是将每个“块”计为单个任务; 然后按需要分配块,而不是单个任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.