[mlpack-svn] r15556 - mlpack/conf/jenkins-conf/benchmark/util

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Fri Jul 26 09:03:22 EDT 2013


Author: marcus
Date: Fri Jul 26 09:03:21 2013
New Revision: 15556

Log:
Add class to handle timeouts.

Modified:
   mlpack/conf/jenkins-conf/benchmark/util/timer.py

Modified: mlpack/conf/jenkins-conf/benchmark/util/timer.py
==============================================================================
--- mlpack/conf/jenkins-conf/benchmark/util/timer.py	(original)
+++ mlpack/conf/jenkins-conf/benchmark/util/timer.py	Fri Jul 26 09:03:21 2013
@@ -7,6 +7,10 @@
 
 from __future__ import with_statement
 import time
+from functools import wraps
+import errno
+import os
+import signal
 
 '''
 This class implements three functions to measure the time.
@@ -28,4 +32,35 @@
   Return the elapsed time of the timer.
   '''
   def ElapsedTime(self):
-    return self.__finish - self.__start
\ No newline at end of file
+    return self.__finish - self.__start
+
+'''
+This Class provides a Timout Error.
+'''
+class TimeoutError(Exception):
+    errno = 23
+
+'''
+This function implements a timeout for a function call.
+
+ at param seconds - The time until the timeout. Default 5000 seconds.
+ at param errorMessage - Message for the Error when the timeout is invoked.
+ at return Timeout error.
+'''
+def timeout(seconds=5000, errorMessage=os.strerror(errno.ETIME)):
+    def decorator(func):
+        def handleTimeout(signum, frame):
+            raise TimeoutError(errorMessage)
+
+        def wrapper(*args, **kwargs):
+            signal.signal(signal.SIGALRM, handleTimeout)
+            signal.setitimer(signal.ITIMER_REAL,seconds)
+            try:
+                result = func(*args, **kwargs)
+            finally:
+                signal.alarm(0)
+            return result
+
+        return wraps(func)(wrapper)
+
+    return decorator



More information about the mlpack-svn mailing list