python - 当活动作业数量小于 N 时添加新任务
问题描述
我正在尝试在一定数量的内核上并行化一些非常耗时的任务。我的目标是尽可能地利用服务器资源。CPU 的总数为 20,但要执行的任务数量要多得多(例如,100)。
每个任务运行的时间不同,因此下面的代码为一定数量的内核留下了空闲而没有工作的机会。
import multiprocessing as mp
def some_task(*args):
# Something happening here
pass
cpu_count = mp.cpu_count()
p = mp.Pool(cpu_count)
n_thread = 1
for something in somethings:
p.apply_async(some_task, args=(something, ))
if n_thread == cpu_count:
p.close()
p.join()
p = mp.Pool(cpu_count)
n_thread = 1
continue
n_thread += 1
p.close()
if n_thread != 1:
p.join()
当活动作业的数量少于 CPU 的数量时,如何编写将启动新任务的代码?
解决方案
推荐阅读
- linux - Presto DB + 在某些 presto 工作人员宕机的情况下我们可以做什么 + 自动启动
- node.js - ReactJS 保护路由和嵌套路由
- ios - Xamarin IOS NSInvalidArgumentException 原因:***
- jsr352 - Java EE Batch (JSR 352) 会成为 quarkus 的一部分吗?
- jenkins - 詹金斯管道 - 删除@tmp文件夹
- jquery - 尝试使用jquery上传图片
- mysql - 更新记录后更新mysql数据库表的列
- vb.net - VB.NET:将日期 dd/mm/yy 转换为 dd/mm/yyyy
- jquery - 通过“标题开头”将点击处理程序添加到元素的父级
- laravel - 如何在 eloquent Orm 中编写复杂的查询?