comparison Sphinx/source/plugins/python.rst @ 591:2397b0e12bc8

solution to avoid deadlock in Python OnChange()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 07 Jan 2021 14:53:36 +0100
parents 404aca6160f0
children 4038ae299b18
comparison
equal deleted inserted replaced
590:404aca6160f0 591:2397b0e12bc8
287 the REST API of Orthanc in the callback. If you have to call other 287 the REST API of Orthanc in the callback. If you have to call other
288 primitives of Orthanc, you should make these calls in a separate 288 primitives of Orthanc, you should make these calls in a separate
289 thread, passing the pending events to be processed through a 289 thread, passing the pending events to be processed through a
290 message queue. 290 message queue.
291 291
292 Here is the template of a possible solution to avoid such deadlocks by
293 relying on the multithreading primitives of Python::
294
295 import orthanc
296 import threading
297
298 def OnChange(changeType, level, resource):
299 # One can safely invoke the "orthanc" module in this function
300 orthanc.LogWarning("Hello world")
301
302 def _OnChange(changeType, level, resource):
303 # Invoke the actual "OnChange()" function in a separate thread
304 t = threading.Timer(0, function = OnChange, args = (changeType, level, resource))
305 t.start()
306
307 orthanc.RegisterOnChangeCallback(_OnChange)
308
309
292 310
293 Accessing the content of a new instance 311 Accessing the content of a new instance
294 ....................................... 312 .......................................
295 313
296 .. highlight:: python 314 .. highlight:: python