fixed freezing Dispatcher

This commit is contained in:
Tom Krüger
2020-12-07 01:03:54 +01:00
parent ac94d79b38
commit 7e76b12b65

View File

@@ -51,11 +51,12 @@ class Dispatcher (threading.Thread):
worker.start() worker.start()
def wait_to_continue(workers, stop_called): def wait_to_continue(workers, stop_called):
any_worker_alive = any(map(lambda w: w.is_alive(), workers)) any_worker_alive = lambda: any(map(lambda w: w.is_alive(), workers))
while any_worker_alive and not stop_called.is_set(): while any_worker_alive() and not stop_called.is_set():
time.sleep(0) time.sleep(0)
waiter = threading.Thread(target=wait_to_continue, waiter = threading.Thread(target=wait_to_continue,
args=(self.__workers, args=(self.__workers,
self.__stop_called)) self.__stop_called))
@@ -67,12 +68,20 @@ class Dispatcher (threading.Thread):
for worker in self.__workers: for worker in self.__workers:
worker.terminate() worker.terminate()
for worker in self.__workers: for worker in self.__workers:
worker.join() worker.join()
def stop(self): def stop(self):
self.__stop_called.set() self.__stop_called.set()
def num_active_workers(self):
count = 0
for worker in self.__workers:
count += 1 if worker.is_alive() else 0
return count
class Worker (multiprocessing.Process): class Worker (multiprocessing.Process):
def __init__(self, exp_mod, exp_plan): def __init__(self, exp_mod, exp_plan):
multiprocessing.Process.__init__(self) multiprocessing.Process.__init__(self)