Back to index

salome-kernel  6.5.0
Classes | Functions | Variables
KERNEL_PY.kernel.threadhelper Namespace Reference

Classes

class  Runner
class  PeriodicTimer

Functions

def testIfContinue
def endedAction
def TEST_PeriodicTimer
def function_ok
def function_with_exception
def infinite_function
def runWithRunner
def runWithTimeout
def TEST_Timeout
def TEST_Runner
def myCallbackFunction
def TEST_runWithCallback

Variables

string __author__ = "gboulant"
string __date__ = "$1 avr. 2010 18:12:38$"
string testfilename = "/tmp/threadhelperTestFile"

Function Documentation

Definition at line 241 of file threadhelper.py.

00241 
00242 def endedAction():
00243     print "FINI"

Fonction qui se termine correctement

Definition at line 250 of file threadhelper.py.

00250 
00251 def function_ok(nbsteps=5):
00252     """
00253     Fonction qui se termine correctement
00254     """
00255     print "D�but"
00256     cnt=0
00257     while ( cnt < nbsteps ):
00258         print "Etape ", cnt
00259         time.sleep(0.6)
00260         cnt+=1
00261 
00262     print "Fin"

Fonction qui aboutie � une lev�e d'exception

Definition at line 263 of file threadhelper.py.

00263 
00264 def function_with_exception():
00265     """
00266     Fonction qui aboutie � une lev�e d'exception
00267     """
00268     print "D�but"
00269     cnt=0
00270     while ( cnt < 5 ):
00271         print "Etape ", cnt
00272         time.sleep(1)
00273         cnt+=1
00274     
00275     raise Exception("erreur d'ex�cution de la fonction")
00276     print "Fin"

fonction de dur�e infinie (tant qu'il y a du courant �l�ctrique) pour
le test du timeout.

Definition at line 277 of file threadhelper.py.

00277 
00278 def infinite_function():
00279     """
00280     fonction de dur�e infinie (tant qu'il y a du courant �l�ctrique) pour
00281     le test du timeout.
00282     """
00283     print "D�but"
00284     cnt=0
00285     while ( 1 ):
00286         print "Etape ", cnt
00287         time.sleep(1)
00288         cnt+=1
00289     
00290     raise Exception("erreur")
00291     print "Fin"
00292 

Definition at line 356 of file threadhelper.py.

00356 
00357 def myCallbackFunction():
00358     print "myCallbackFunction: the job is ended"
00359     

Ex�cute la fonction avec le runner. On illustre ici la modalit�
d'utilisation du Runner.

Definition at line 293 of file threadhelper.py.

00293 
00294 def runWithRunner(functionToRun):
00295     """
00296     Ex�cute la fonction avec le runner. On illustre ici la modalit�
00297     d'utilisation du Runner.
00298     """
00299     print "###########"
00300     runner = Runner(functionToRun)
00301     runner.start()
00302 
00303     while ( not runner.isEnded() ):
00304         print "La fonction est en cours"
00305         time.sleep(0.2)
00306     e = runner.getException()
00307     if e is not None:
00308         print "La fonction s'est termin�e en erreur"
00309         print e
00310         # On peut en fait la relancer
00311         # raise e
00312     else:
00313         print "La fonction s'est termin�e correctement"
00314 

Here is the caller graph for this function:

def KERNEL_PY.kernel.threadhelper.runWithTimeout (   functionToRun,
  timeout = 10 
)
Ex�cute la fonction avec le runner. On illustre ici la modalit�
d'utilisation du Runner.

Definition at line 315 of file threadhelper.py.

00315 
00316 def runWithTimeout(functionToRun, timeout=10):
00317     """
00318     Ex�cute la fonction avec le runner. On illustre ici la modalit�
00319     d'utilisation du Runner.
00320     """
00321     print "runWithTimeout : DEBUT"
00322     runner = Runner(functionToRun)
00323     runner.start()
00324 
00325     # On se fixe un temps au del� duquel on consid�re que la fonction
00326     # est en erreur => on tue le thread (timeout)
00327     runner.wait(timeout)
00328     print "Apr�s runner.timeout(timeout)"
00329     if not runner.isEnded():    
00330         runner.kill()
00331     e = runner.getException()
00332     if e is not None:
00333         print "La fonction s'est termin�e en erreur"
00334         print e
00335         # On peut en fait la relancer
00336         # raise e
00337     else:
00338         print "La fonction s'est termin�e correctement"
00339 
00340     print "runWithTimeout : FIN"
00341     import sys
00342     sys.exit(0)
00343     

Here is the caller graph for this function:

Definition at line 244 of file threadhelper.py.

00244 
00245 def TEST_PeriodicTimer():
00246     periodicTimer=PeriodicTimer(1,0,testIfContinue, endedAction)
00247     periodicTimer.start()
00248 
00249 
# ======================================================

Definition at line 350 of file threadhelper.py.

00350 
00351 def TEST_Runner():
00352     runWithRunner(function_ok)
00353     runWithRunner(function_with_exception)
00354     #runWithRunner(infinite_function)
00355 

Here is the call graph for this function:

Definition at line 360 of file threadhelper.py.

00360 
00361 def TEST_runWithCallback():
00362     runner = Runner(function_ok,8)
00363     runner.setCallbackFunction(myCallbackFunction)
00364     runner.start()
00365 
00366     if runner.getException() is not None:
00367         return False
00368 
00369     runnerId = runner.getId()
00370     print "A runner has been started with id="+str(runnerId)
00371     cpt = 0
00372     while ( not runner.isEnded() ):
00373         print "Waiting notification from process "+str(runner.getId())+", step n°"+str(cpt)
00374         time.sleep(0.2)
00375         cpt+=1
00376 
00377     return True

Definition at line 344 of file threadhelper.py.

00344 
00345 def TEST_Timeout():
00346     #runWithTimeout(function_ok)
00347     #runWithTimeout(function_ok,2)
00348     runWithTimeout(function_with_exception)
00349 
    

Here is the call graph for this function:

Definition at line 234 of file threadhelper.py.

00234 
00235 def testIfContinue():
00236     print "On examine la pr�sence du fichier ", testfilename
00237     if os.path.exists(testfilename):
00238         return STOP
00239     else:
00240         return CONTINUE


Variable Documentation

Definition at line 24 of file threadhelper.py.

string KERNEL_PY.kernel.threadhelper.__date__ = "$1 avr. 2010 18:12:38$"

Definition at line 25 of file threadhelper.py.

string KERNEL_PY.kernel.threadhelper.testfilename = "/tmp/threadhelperTestFile"

Definition at line 233 of file threadhelper.py.