[英]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.