py.lib
2020-07-24
Child:1668cc57225b
py.lib/Alarm.py
.. Реструктуризация
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/Alarm.py Fri Jul 24 14:40:37 2020 +0300 1.3 @@ -0,0 +1,25 @@ 1.4 +#!/usr/bin/python 1.5 +# -*- coding: utf-8 -*- 1.6 + 1.7 +from signal import SIGALRM, alarm, signal 1.8 + 1.9 +class AlarmTimeout(Exception): pass 1.10 + 1.11 +def _handler(sig, frame): 1.12 + raise AlarmTimeout('Operation timeout') 1.13 + 1.14 +class MkAlarm(object): 1.15 + def __init__(self, timeout): 1.16 + signal(SIGALRM, _handler) 1.17 + alarm(timeout) 1.18 + 1.19 + def __enter__(self): 1.20 + pass 1.21 + 1.22 + def __exit__(self, eType, eObj, tb): 1.23 + if eObj == None: 1.24 + alarm(0) 1.25 + 1.26 + def __del__(self): 1.27 + alarm(0) 1.28 + 1.29 \ No newline at end of file