Source code for pipeLionServer.utils.decorators
""" Module holds different decorators, which can be used within the pipeLionServer
"""
import signal
import logging
import time
import os
from pipeLionServer.core.error import PipeLionServerTimeoutException
from pipeLion.utils.platformUtils import currentPlatform
_log = logging.getLogger('pipeLionServer')
[docs]def timeout(timeout_time, *args, **kwargs):
""" this decorator will return a PipeLionServerTimeoutException if the call of the decorated method takes longer as
the given time.
:param timeout_time: seconds to timeout
:type timeout_time: int
:returns: returnValue of the decorated method or PipeLionServerTimeoutException if the call takes longer than the given time
:rtype: PipeLionServerTimeoutException or variant
"""
def timeout_function(f):
def handler(signum, frame):
raise PipeLionServerTimeoutException()
oldHandler = signal.getsignal(signal.SIGABRT)
signal.signal(signal.SIGABRT, handler)
def f2(*args):
import threading
class InterruptableThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.evt = threading.Event()
self.evt.clear()
def run(self):
for i in xrange(timeout_time*10000):
time.sleep(.0001)
if self.evt.isSet():
return
pid = os.getpid()
os.kill(pid, signal.SIGABRT)
it = InterruptableThread()
result = PipeLionServerTimeoutException
it.start()
try:
result = f(*args, **kwargs)
it.evt.set()
it.join()
except:
_log.critical('there was a timeoutException')
signal.signal(signal.SIGABRT, oldHandler)
return result
return f2
return timeout_function