aboutsummaryrefslogtreecommitdiff
path: root/pyecsca/misc
diff options
context:
space:
mode:
authorJ08nY2024-04-03 14:37:17 +0200
committerJ08nY2024-04-03 14:37:17 +0200
commita115539931b4ca03f4009fcfaccc1a4c18517e58 (patch)
treedcd131c94557874e3d0af93b5d47f3a0debc6756 /pyecsca/misc
parentc91f698a8a56fb7e0e7e30a544be2c375caf13e0 (diff)
downloadpyecsca-a115539931b4ca03f4009fcfaccc1a4c18517e58.tar.gz
pyecsca-a115539931b4ca03f4009fcfaccc1a4c18517e58.tar.zst
pyecsca-a115539931b4ca03f4009fcfaccc1a4c18517e58.zip
Diffstat (limited to 'pyecsca/misc')
-rw-r--r--pyecsca/misc/utils.py25
1 files changed, 24 insertions, 1 deletions
diff --git a/pyecsca/misc/utils.py b/pyecsca/misc/utils.py
index 4e6a26b..aa450e3 100644
--- a/pyecsca/misc/utils.py
+++ b/pyecsca/misc/utils.py
@@ -1,9 +1,12 @@
"""Just some utilities I promise."""
import sys
from ast import parse
+from typing import List, Any, Generator
from ..misc.cfg import getconfig
+from concurrent.futures import ProcessPoolExecutor, as_completed, Future
+
def pexec(s):
return parse(s, mode="exec")
@@ -17,7 +20,8 @@ def in_notebook() -> bool:
"""Test whether we are executing in Jupyter notebook."""
try:
from IPython import get_ipython
- if 'IPKernelApp' not in get_ipython().config: # pragma: no cover
+
+ if "IPKernelApp" not in get_ipython().config: # pragma: no cover
return False
except ImportError:
return False
@@ -36,3 +40,22 @@ def warn(*args, **kwargs):
"""Log a message."""
if in_notebook() and getconfig().log.enabled:
print(*args, **kwargs, file=sys.stderr)
+
+
+class TaskExecutor(ProcessPoolExecutor):
+ """A simple ProcessPoolExecutor that keeps tracks of tasks that were submitted to it."""
+ keys: List[Any]
+ futures: List[Future]
+
+ def submit_task(self, key: Any, fn, /, *args, **kwargs):
+ self.futures.append(self.submit(fn, *args, **kwargs))
+ self.keys.append(key)
+
+ @property
+ def tasks(self):
+ return list(zip(self.keys, self.futures))
+
+ def as_completed(self) -> Generator[tuple[Any, Future], Any, None]:
+ for future in as_completed(self.futures):
+ i = self.futures.index(future)
+ yield self.keys[i], future