AsyncCallThread.py

Go to the documentation of this file.
00001 #  This module was kindly provided by the Ganga developer
00002 #
00003 #    Jakub (Kuba) Moscicki Jakub.Moscicki@cern.ch
00004 #
00005 #  to provide a timeout wrapper for commands that might possibly hang.
00006 
00007 from threading import Thread
00008 import time
00009 
00010 class AsyncCallThread(Thread):
00011     """Asynchronous function call thread.  The result of call
00012     func(*args,**kwds) is stored in the result attribute.  If an exception is
00013     raised the exception object is stored in the exception
00014     attribute. Otherwise the exception attribute is None.  The caller is
00015     notified with the callback(t) where t is this thread object.
00016     """
00017     def __init__(self, callback, func, *args, **kwds):
00018         Thread.__init__(self,name = 'async_call_%s'%func.__name__)
00019         self.callback = callback
00020         self.func = func
00021         self.args = args
00022         self.kwds = kwds
00023         self.result = None
00024         self.exception = None
00025         self.start_time = 0
00026         self.stop_time = 0
00027         self.setDaemon(True)  #This allows python to end even if thread still active.
00028         
00029     def run(self):
00030         self.start_time = time.time()
00031 
00032         try:
00033             print "Making asynchronous call:",self.func.__name__,self.args,self.kwds
00034             self.result = self.func(*self.args,**self.kwds)
00035         except Exception,x:
00036             self.exception = x
00037 
00038         self.stop_time = time.time()
00039         self.callback(self)
00040 
00041         
00042 def async_call(callback,func, *args, **kwds):
00043     """Call func asynchronously in a separate thread. See AsyncCallThread class for details."""
00044     t = AsyncCallThread(callback,func,*args,**kwds)
00045     t.start()
00046     return t
00047 
00048 
00049 def timeout_call(caller,func, timeout, *args, **kwds):
00050     def callback(t):
00051         print 'completed'
00052 
00053     t = AsyncCallThread(callback,func,*args,**kwds)
00054     t.start()
00055     time.sleep(1)
00056     while time.time()-t.start_time < timeout:
00057         if t.stop_time:
00058             if t.exception:
00059                 raise t.exception
00060             else:
00061                 return t.result
00062 
00063         time.sleep(1)
00064 
00065     #  Extension to Kuba's code
00066 
00067     from GBSLogger    import GetLoggerThreshold, Log, logger 
00068     from GBSUtilities import RemoveChildProcesses
00069     from GBSTimeStamp import GBSTimeStamp
00070     
00071     Log(caller,logger.ERROR,"Timeout reached before asynchronous call ended ...")
00072     Log(caller,logger.ERROR,"...Attempting to delete child processes to unblock before raising timeout exception...")
00073     RemoveChildProcesses()    
00074     raise Exception('timeout!')
00075 

Generated on Fri Mar 5 09:25:41 2010 for gbs by  doxygen 1.4.7