From 1eff542a2437ff6c2ec664fcff668124ff0d4660 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Jan=C3=9Fen?= Date: Tue, 7 Nov 2023 21:03:24 +0100 Subject: [PATCH 1/4] Add __del__() for Executors --- pympipool/shared/executorbase.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pympipool/shared/executorbase.py b/pympipool/shared/executorbase.py index 9e75f31a..810ecc37 100644 --- a/pympipool/shared/executorbase.py +++ b/pympipool/shared/executorbase.py @@ -55,10 +55,15 @@ def shutdown(self, wait=True, *, cancel_futures=False): self._future_queue.put({"shutdown": True, "wait": wait}) self._process.join() self._future_queue.join() + self._process = None def __len__(self): return self._future_queue.qsize() + def __del__(self): + if self._process is not None: + self.shutdown(wait=True) + def _set_init_function(self, init_function): if init_function is not None: self._future_queue.put( From 0163c4bfd75f993053c79b8f83e7dc1b5ee46c8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Jan=C3=9Fen?= Date: Tue, 7 Nov 2023 21:13:47 +0100 Subject: [PATCH 2/4] check if attribute is available --- pympipool/shared/executorbase.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pympipool/shared/executorbase.py b/pympipool/shared/executorbase.py index 810ecc37..2f2e81e7 100644 --- a/pympipool/shared/executorbase.py +++ b/pympipool/shared/executorbase.py @@ -61,7 +61,7 @@ def __len__(self): return self._future_queue.qsize() def __del__(self): - if self._process is not None: + if hasattr(self, "_process") and self._process is not None: self.shutdown(wait=True) def _set_init_function(self, init_function): From 49e9a173dfe2d27566fdcf6a91c08acd00d73f59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Jan=C3=9Fen?= Date: Tue, 7 Nov 2023 21:25:00 +0100 Subject: [PATCH 3/4] if no process is defined do not try to join it --- pympipool/shared/executorbase.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pympipool/shared/executorbase.py b/pympipool/shared/executorbase.py index 2f2e81e7..2c672c2c 100644 --- a/pympipool/shared/executorbase.py +++ b/pympipool/shared/executorbase.py @@ -53,7 +53,8 @@ def shutdown(self, wait=True, *, cancel_futures=False): if cancel_futures: cancel_items_in_queue(que=self._future_queue) self._future_queue.put({"shutdown": True, "wait": wait}) - self._process.join() + if self._process is not None: + self._process.join() self._future_queue.join() self._process = None From 7ac050135458c1dcd7dfd22454158890fbad1afa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Jan=C3=9Fen?= Date: Tue, 7 Nov 2023 21:30:06 +0100 Subject: [PATCH 4/4] handle exceptions --- pympipool/shared/executorbase.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pympipool/shared/executorbase.py b/pympipool/shared/executorbase.py index 2c672c2c..8e64096a 100644 --- a/pympipool/shared/executorbase.py +++ b/pympipool/shared/executorbase.py @@ -53,8 +53,7 @@ def shutdown(self, wait=True, *, cancel_futures=False): if cancel_futures: cancel_items_in_queue(que=self._future_queue) self._future_queue.put({"shutdown": True, "wait": wait}) - if self._process is not None: - self._process.join() + self._process.join() self._future_queue.join() self._process = None @@ -62,8 +61,10 @@ def __len__(self): return self._future_queue.qsize() def __del__(self): - if hasattr(self, "_process") and self._process is not None: + try: self.shutdown(wait=True) + except (AttributeError, RuntimeError): + pass def _set_init_function(self, init_function): if init_function is not None: