py.lib

Yohn Y. 2022-02-23 Parent:cab7fedf8432 Child:ff755f64cda8

23:1668cc57225b Go to Latest

py.lib/Alarm.py

. Рефакторинг бессмысленный и беспощадный

History
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
4 from signal import SIGALRM, alarm, signal
7 class AlarmTimeout(Exception): pass
10 def _handler(sig, frame):
11 raise AlarmTimeout('Operation timeout')
14 class Alarm(object):
15 def __init__(self, timeout):
16 signal(SIGALRM, _handler)
17 alarm(timeout)
19 def __enter__(self):
20 pass
22 def __exit__(self, e_type, e_obj, tb):
23 if e_obj is None:
24 alarm(0)
26 def __del__(self):
27 alarm(0)