繁体   English   中英

使用 pool.map_async 在 python 中进行多处理

[英]multiprocessing in python using pool.map_async

嗨,我觉得我不太了解 python 中的多处理。

我想并行运行一个名为“run_worker”的 function(它只是运行和管理子进程的代码)20 次,并等待所有功能完成。 每个 run_worker 应该在单独的核心/线程上运行。 我不介意流程完成的顺序,因此我使用了异步并且我没有返回值,所以我使用了 map

我认为我应该使用:

if __name__ == "__main__":
    num_workers = 20
    param_map = []
    for i in range(num_workers):
        param_map += [experiment_id]
        
    pool = mp.Pool(processes= num_workers)
    pool.map_async(run_worker, param_map)
    
    pool.close()
    pool.join()

However this code exits straight away and doesn't appear to execute run_worker properly. Also do I really have to create a param_map of the same experiment_id to pass to the worker because this seems like a hack to get the number of run_workers created. Ideally i would like to run a function with no parameters and no return value over multiple cores. 

注意我在 AWS 中使用 windows 2019 服务器。

编辑添加了 run_worker ,它调用一个写入文件的子进程:

def run_worker(experiment_id):
    hostname = socket.gethostname()
    experiment = conn.experiments(experiment_id).fetch()

    while experiment.progress.observation_count < experiment.observation_budget:
        suggestion = conn.experiments(experiment.id).suggestions().create()
        value = evaluate_model(suggestion.assignments)
        conn.experiments(experiment_id).observations().create(suggestion=suggestion.id,value=value,metadata=dict(hostname=hostname),)
        # Update the experiment object
        experiment = conn.experiments(experiment_id).fetch()

似乎出于这个简单的目的,您最好使用pool.map而不是pool.map_async 它们都并行运行,但是pool.map会阻塞,直到所有操作完成(另请参阅此问题)。 pool.map_async特别适用于以下情况:

result = map_async(func, iterable)
while not result.ready():
    // do some work while map_async is running
    pass

// blocking call to get the result
out = result.get()

关于您关于参数的问题,map 操作的基本思想是 map 将一个列表/数组/可迭代到相同大小的新值列表的值。 据我在文档中看到的, multiprocessing没有提供任何方法来运行没有参数的多个函数。

如果您还想分享您的run_worker function,这可能有助于更好地回答您的问题。 这也可以解释为什么你会在没有任何 arguments 的情况下运行 function 并首先使用map操作返回值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM