diff options
| author | 2019-09-17 11:56:30 +0100 | |
|---|---|---|
| committer | 2019-09-17 11:56:30 +0100 | |
| commit | 1ed14f9a1706b21131063589cdfee5e6b3e4c9c4 (patch) | |
| tree | 1320942e1704bcdea6bd304330937c827dd97e87 | |
| parent | allow per-server default mumble servers (diff) | |
| signature | ||
first draft of multiprocess.Process deadline system
| -rw-r--r-- | src/utils/__init__.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/utils/__init__.py b/src/utils/__init__.py index 4ad6ce46..2177bbf1 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -383,3 +383,26 @@ def deadline(seconds: int=10): finally: signal.signal(signal.SIGALRM, old_handler) signal.setitimer(signal.ITIMER_REAL, old_seconds, 0) + +def deadline_process(func: typing.Callable[[], None], seconds: int=10): + q = multiprocessing.Queue() + def _wrap(func, q): + try: + q.put([True, func()]) + except Exception as e: + print(e) + q.put([False, e]) + + p = multiprocessing.Process(target=_wrap, args=(func, q)) + p.start() + p.join(seconds) + + if p.is_alive(): + p.terminate() + _raise_deadline() + + success, out = q.get(block=False) + if success: + return out + else: + raise out |
