mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 09:53:32 +01:00
Add Process and Signals support
Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
6123a68195
commit
c96ffe52c0
6 changed files with 139 additions and 0 deletions
34
tools/python/jwutils/Signals.py
Normal file
34
tools/python/jwutils/Signals.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
from abc import ABC, abstractmethod
|
||||
|
||||
_handled_signals = {}
|
||||
|
||||
def _signal_handler(signal, frame):
|
||||
if not signal in _handled_signals.keys():
|
||||
return
|
||||
for h in _handled_signals[signal]:
|
||||
h.func(signal, *h.args)
|
||||
|
||||
class Signals:
|
||||
|
||||
class Handler:
|
||||
def __init__(self, func, args):
|
||||
self.func = func
|
||||
self.args = args
|
||||
|
||||
def __init(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
@abstractmethod
|
||||
def _add_handler(self, signal, handler):
|
||||
raise Exception("_add_handler() is not reimplemented")
|
||||
|
||||
@classmethod
|
||||
def add_handler(cls, signals, handler, *args):
|
||||
for signal in signals:
|
||||
h = Signals.Handler(handler, args)
|
||||
if not signal in _handled_signals.keys():
|
||||
_handled_signals[signal] = [h]
|
||||
cls._add_signal_handler(signal, _signal_handler)
|
||||
else:
|
||||
_handled_signals[signal].add(h)
|
||||
Loading…
Add table
Add a link
Reference in a new issue