mirror of
https://github.com/OpenMTC/OpenMTC.git
synced 2025-02-24 18:10:58 +00:00
30 lines
467 B
Python
30 lines
467 B
Python
'''
|
|
Created on 20.05.2011
|
|
|
|
@author: kca
|
|
'''
|
|
|
|
from signal import signal, SIGALRM, alarm
|
|
from contextlib import contextmanager
|
|
from futile import noop
|
|
|
|
|
|
@contextmanager
|
|
def timeout(seconds):
|
|
if not seconds:
|
|
yield
|
|
return
|
|
|
|
original_handler = signal(SIGALRM, noop)
|
|
|
|
try:
|
|
alarm(seconds)
|
|
yield
|
|
finally:
|
|
alarm(0)
|
|
signal(SIGALRM, original_handler)
|
|
|
|
|
|
def Timeout(seconds):
|
|
return lambda: timeout(seconds)
|