# HG changeset patch # User Sebastien Jodogne # Date 1586075136 -7200 # Node ID 8479963946887aa7ef048b52045773139b95639c # Parent 8f7e9ebf7c96e6857da18c985a4ae8a49e77077e Scheduling a task for periodic execution diff -r 8f7e9ebf7c96 -r 847996394688 Sphinx/source/plugins/python.rst --- a/Sphinx/source/plugins/python.rst Thu Apr 02 08:40:20 2020 +0200 +++ b/Sphinx/source/plugins/python.rst Sun Apr 05 10:25:36 2020 +0200 @@ -380,7 +380,42 @@ print(' - Method %s(): %s' % (subname, inspect.getdoc(subobj))) print('') - + +.. _python-scheduler: + +Scheduling a task for periodic execution +........................................ + +.. highlight:: python + +The following Python script will periodically (every second) run the +function ``Hello()`` thanks to the ``threading`` module:: + + import orthanc + import threading + + TIMER = None + + def Hello(): + global TIMER + TIMER = None + orthanc.LogWarning("In Hello()") + # Do stuff... + TIMER = threading.Timer(1, Hello) # Re-schedule after 1 second + TIMER.start() + + def OnChange(changeType, level, resource): + if changeType == orthanc.ChangeType.ORTHANC_STARTED: + orthanc.LogWarning("Starting the scheduler") + Hello() + + elif changeType == orthanc.ChangeType.ORTHANC_STOPPED: + if TIMER != None: + orthanc.LogWarning("Stopping the scheduler") + TIMER.cancel() + + orthanc.RegisterOnChangeCallback(OnChange) +