#!/usr/bin/env python ########################################################## # TIMEOUT v2.0 # ########################################################## # Author: Michal Bielecki # # Date: Szczecin, 17 de abril de 2007 # # Description: Class to control Timeout of a function # # Version: 2007060500 # # # # Codigo fuente bajo licencia GNU/GPL # # Centrologic (Computational Logistic Center) # # http://www.centrologic.com - info@centrologic.com # ########################################################## # Libraries to use import signal import time # Exception TimedOutExc class TimedOutExc(Exception): def __init__(self, value = "Timed Out"): self.value = value def __str__(self): return repr(self.value) # Timeout function ## Timeout function ## \param f Function to process ## \param timeout Time to wait until Timeout ## \param *args - ## \param **kwargs - def TimedOutFn(f, timeout, *args, **kwargs): def handler(signum, frame): raise TimedOutExc() old = signal.signal(signal.SIGALRM, handler) signal.alarm(timeout) try: result = f(*args, **kwargs) finally: signal.signal(signal.SIGALRM, old) signal.alarm(0) return result