Closed mandarup closed 3 years ago
it seems like the issue with passing lock to the child process
def child_proc_simple(task_id):
#time.sleep(1)
print(task_id)
def child_proc_simple_with_lock(lock, task_id):
#time.sleep(1)
print(task_id)
def simple():
with mproc.Manager() as manager:
# create a variable shared across all threads
shared_best_result = manager.dict()
best_results_schema = {
'task_id': None,
'time': None,
'best': np.inf,
}
for k,v in best_results_schema.items():
shared_best_result[k] = v
lock = manager.Lock()
with manager.Pool(processes=5) as pool:
# this works: <------- WORKS
result = pool.starmap(child_proc_simple, [(
lock,
task_id,
) for task_id in range(10)])
# this does not work, gets stuck <------- FAILS
result = pool.starmap(child_proc_simple_with_lock, [(
lock,
task_id,
) for task_id in range(10)])
logger.info(shared_best_result)
# convert Manager.dict to python's dict before exiting Manager context
result = dict(shared_best_result)
return result
Using processes directly seems to work.
with mproc.Manager() as manager:
# create a variable shared across all threads
shared_best_result = manager.dict()
best_results_schema = {
'task_id': None,
'time': None,
'best': 1e6,
}
for k, v in best_results_schema.items():
shared_best_result[k] = v
lock = manager.Lock()
processes = [
mproc.Process(target=child_proc,
args=(lock, task_id, shared_best_result,))
for task_id in range(10)
]
for process in processes:
process.start()
for process in processes:
process.join()
this is a workaround, closing this for now.
Functional example below works with
multiprocessing
but fails (gets stuck at pool.starmap) withbilliard
. Is there anything wrong with this example, does it need to be adapted to billiard somehow?billiard==3.6.4.0 python3.9.0 MacOS Catalina
Thanks for help!!