-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib_mpex.py
57 lines (46 loc) · 1.41 KB
/
lib_mpex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import multiprocessing
import sys
from abc import ABC, abstractmethod
from dataclasses import dataclass
from types import TracebackType
from typing import Type, Optional
from tblib import pickling_support
pickling_support.install()
@dataclass
class ChildExit:
exc_info: (
tuple[Type[BaseException], BaseException, TracebackType]
| tuple[None, None, None]
)
pid: int
class_name: str
error: str
def is_exc(self) -> bool:
return self.exc_info[0] is not None
class ChildProcess(ABC):
@abstractmethod
def _run(self):
raise NotImplementedError
def run(self, ex_queue: multiprocessing.Queue):
ex_record: Optional[ChildExit] = None
try:
self._run()
except Exception as e:
ex_record = ChildExit(
exc_info=sys.exc_info(),
pid=multiprocessing.current_process().pid,
class_name=self.__class__.__name__,
error=str(e),
)
if not ex_record:
ex_record = ChildExit(
exc_info=(None, None, None),
pid=multiprocessing.current_process().pid,
class_name=self.__class__.__name__,
error="exited normally.",
)
try:
ex_queue.put(ex_record)
except Exception as e:
print("PANIC:ex_queue.put exception:", e)
sys.exit(1)