Fix hanging bug by adding manager

This commit is contained in:
Andras Schmelczer 2022-07-03 16:01:52 +02:00
parent ad1b2a20ef
commit 2f4d203446
10 changed files with 40 additions and 30 deletions

View file

@ -7,6 +7,7 @@ from .get_config import get_config
from .manage_communication import manage_communication
from .manage_serial import manage_serial
from .mapper_function import mapper_function
from .worker_exception import WorkerException
T = TypeVar("T")
V = TypeVar("V")
@ -87,13 +88,17 @@ def parallel_map(
ignore_exceptions=ignore_exceptions,
)
ctx = mp.get_context("spawn")
ctx = (
mp.get_context("fork")
if "fork" in mp.get_all_start_methods()
else mp.get_context("spawn")
)
ctx.freeze_support()
manager = ctx.Manager()
input_queue = manager.Queue(config.concurrency * 2)
output_queue = manager.Queue(config.concurrency * 2)
input_queue = ctx.Queue(config.concurrency * 2)
output_queue = ctx.Queue(config.concurrency * 2)
should_stop = ctx.Event()
serialized_map_function = dill.dumps(function, byref=True, recurse=True)
processes = [
@ -124,13 +129,17 @@ def parallel_map(
ignore_exceptions=ignore_exceptions,
)
should_stop.set()
except:
except WorkerException:
should_stop.set()
raise
except Exception:
for p in processes:
p.terminate()
p.kill()
raise
finally:
for p in processes:
p.join() # terminated processes have to be joined else they remain zombies
p.close()
input_queue.close()
output_queue.close()
manager.shutdown()