generateur_v3/backend/api/services/timeout.py

27 lines
730 B
Python
Raw Normal View History

2022-09-16 21:50:55 +02:00
from contextlib import contextmanager
import signal
2022-10-10 01:34:38 +02:00
import multiprocessing as mp
import traceback
2022-09-16 21:50:55 +02:00
2022-10-10 01:34:38 +02:00
class Process(mp.Process):
def __init__(self, *args, **kwargs):
mp.Process.__init__(self, *args, **kwargs)
self._pconn, self._cconn = mp.Pipe()
self._exception = None
def run(self):
try:
mp.Process.run(self)
self._cconn.send(None)
except Exception as e:
tb = traceback.format_exc()
self._cconn.send((e, tb))
# raise e # You can still rise this exception if you need to
@property
def exception(self):
if self._pconn.poll():
self._exception = self._pconn.recv()
return self._exception