-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_handler.py
32 lines (26 loc) · 988 Bytes
/
process_handler.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
import os
import signal
from functools import partial
from asyncio import get_event_loop, gather
from concurrent.futures import ThreadPoolExecutor
class ProcessHandler:
def __init__(self):
self.pool = ThreadPoolExecutor((os.cpu_count() or 1))
self.loop = get_event_loop()
@staticmethod
async def get_executor(handlers):
executor = await gather(*handlers, return_exceptions=True)
return executor
def start(self, handlers, cleanup_callback):
for sig_name in {'SIGINT', 'SIGTERM'}:
self.loop.add_signal_handler(
getattr(signal, sig_name),
partial(self.shutdown, sig_name, cleanup_callback)
)
self.loop.run_until_complete(self.get_executor(handlers))
self.loop.run_forever()
self.loop.stop()
def shutdown(self, sig_name, cleanup_callback):
print("Got signal %s. Exiting..." % sig_name)
cleanup_callback()
self.loop.stop()