comparison Sphinx/source/plugins/python.rst @ 373:847996394688

Scheduling a task for periodic execution
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sun, 05 Apr 2020 10:25:36 +0200
parents 181b02cea7ab
children f5bbdabb04d9
comparison
equal deleted inserted replaced
370:8f7e9ebf7c96 373:847996394688
378 elif (not subname.startswith('_') and 378 elif (not subname.startswith('_') and
379 inspect.ismethoddescriptor(subobj)): 379 inspect.ismethoddescriptor(subobj)):
380 print(' - Method %s(): %s' % (subname, inspect.getdoc(subobj))) 380 print(' - Method %s(): %s' % (subname, inspect.getdoc(subobj)))
381 print('') 381 print('')
382 382
383 383
384 .. _python-scheduler:
385
386 Scheduling a task for periodic execution
387 ........................................
388
389 .. highlight:: python
390
391 The following Python script will periodically (every second) run the
392 function ``Hello()`` thanks to the ``threading`` module::
393
394 import orthanc
395 import threading
396
397 TIMER = None
398
399 def Hello():
400 global TIMER
401 TIMER = None
402 orthanc.LogWarning("In Hello()")
403 # Do stuff...
404 TIMER = threading.Timer(1, Hello) # Re-schedule after 1 second
405 TIMER.start()
406
407 def OnChange(changeType, level, resource):
408 if changeType == orthanc.ChangeType.ORTHANC_STARTED:
409 orthanc.LogWarning("Starting the scheduler")
410 Hello()
411
412 elif changeType == orthanc.ChangeType.ORTHANC_STOPPED:
413 if TIMER != None:
414 orthanc.LogWarning("Stopping the scheduler")
415 TIMER.cancel()
416
417 orthanc.RegisterOnChangeCallback(OnChange)
418
384 419
385 420
386 421
387 Performance and concurrency 422 Performance and concurrency
388 --------------------------- 423 ---------------------------