@@ -1212,24 +1212,45 @@ class AsynchronousFunction(threading.Thread):
12121212 threading.Event().wait(0.1)
12131213 print 'result = ',result
12141214 '''
1215- def __init__ (self ,function ):
1216- """It just creates the function object, you must call function.start() afterwards"""
1215+ def __init__ (self , function , args = None , kwargs = None ,
1216+ callback = None , pause = 0.0 , start = False ,
1217+ ):
1218+ """
1219+ It just creates the function object.
1220+ If pause!=0 or start=True, the function will be called
1221+ """
12171222 self .function = function
12181223 self .result = None
12191224 self .exception = None
12201225 self .finished = threading .Event ()
12211226 self .finished .clear ()
12221227 threading .Thread .__init__ (self )
1228+ self .callback = callback
1229+ self .pause = pause
12231230 self .wait = self .finished .wait
12241231 self .daemon = False
1232+ self .args = args or []
1233+ self .kwargs = kwargs or {}
1234+ if self .pause or start :
1235+ self .start ()
1236+
12251237 def run (self ):
12261238 try :
1227- self .wait (0.01 )
1228- self .result = self .function ()
1239+ if self .pause :
1240+ self .wait (self .pause )
1241+ self .result = self .function (* self .args , ** self .kwargs )
12291242 except Exception ,e :
12301243 self .result = None
12311244 self .exception = e
1232- self .finished .set () #Not really needed, simply call AsynchronousFunction.isAlive() to know if it has finished
1245+ # Not really needed, simply call AsynchronousFunction.isAlive()
1246+ # to know if it has finished
1247+ self .finished .set ()
1248+ if self .callback :
1249+ try :
1250+ self ._bg = AsynchronousFunction (self .callback , start = True ,
1251+ args = [self .result ] if self .result is not None else [])
1252+ except :
1253+ traceback .print_exc ()
12331254
12341255from . import doc
12351256__doc__ = doc .get_fn_autodoc (__name__ ,vars ())
0 commit comments