parallel.py 文件源码

python
阅读 33 收藏 0 点赞 0 评论 0

项目:deepjets 作者: deepjets 项目源码 文件源码
def run_pool(workers, n_jobs=-1, sleep=0.1):
    # defensive copy
    workers = workers[:]
    if n_jobs < 1:
        n_jobs = multiprocessing.cpu_count()
    processes = []
    p = None
    try:
        while True:
            active = multiprocessing.active_children()
            while len(active) < n_jobs and len(workers) > 0:
                p = workers.pop(0)
                p.start()
                processes.append(p)
                active = multiprocessing.active_children()
            if len(workers) == 0 and len(active) == 0:
                break
            time.sleep(sleep)
    except (KeyboardInterrupt, SystemExit):
        if p is not None:
            p.terminate()
        for p in processes:
            p.terminate()
        raise
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号