comparison Sphinx/source/plugins/python.rst @ 600:4038ae299b18

note about deadlocks in python plugins
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 21 Jan 2021 18:40:56 +0100
parents 2397b0e12bc8
children eaa6cdfa7ba6
comparison
equal deleted inserted replaced
599:1248dadca1cd 600:4038ae299b18
277 orthanc.RegisterOnChangeCallback(OnChange) 277 orthanc.RegisterOnChangeCallback(OnChange)
278 278
279 279
280 280
281 .. warning:: 281 .. warning::
282 Your callback function will be called synchronously with 282 In releases <= 3.0 of the Python plugin, deadlocks might emerge if
283 the core of Orthanc. This implies that deadlocks might emerge if 283 you call other core primitives of Orthanc (such as the REST API) in
284 you call other core primitives of Orthanc in your callback (such 284 your callback function. This issue has been `fixed in release 3.1
285 deadlocks are particular visible in the presence of other plugins 285 <https://hg.orthanc-server.com/orthanc-python/rev/46fe70776d61>`__.
286 or Lua scripts). It is thus strongly advised to avoid any call to 286
287 the REST API of Orthanc in the callback. If you have to call other 287 As a **temporary workaround** against such deadlocks in releases <=
288 primitives of Orthanc, you should make these calls in a separate 288 3.0, if you have to call other primitives of Orthanc, you should make
289 thread, passing the pending events to be processed through a 289 these calls in a separate thread, passing the pending events to be
290 message queue. 290 processed through a message queue. Here is the template of a possible
291 291 solution to postpone such deadlocks as much as possible by relying on
292 Here is the template of a possible solution to avoid such deadlocks by 292 the multithreading primitives of Python::
293 relying on the multithreading primitives of Python::
294 293
295 import orthanc 294 import orthanc
296 import threading 295 import threading
297 296
298 def OnChange(changeType, level, resource): 297 def OnChange(changeType, level, resource):
303 # Invoke the actual "OnChange()" function in a separate thread 302 # Invoke the actual "OnChange()" function in a separate thread
304 t = threading.Timer(0, function = OnChange, args = (changeType, level, resource)) 303 t = threading.Timer(0, function = OnChange, args = (changeType, level, resource))
305 t.start() 304 t.start()
306 305
307 orthanc.RegisterOnChangeCallback(_OnChange) 306 orthanc.RegisterOnChangeCallback(_OnChange)
307
308
309 Beware that **this workaround is imperfect** and deadlocks have been
310 observed even if using it! Make sure to upgrade your plugin to solve
311 this issue for good. Note that this temporary workaround is not needed
312 in releases >= 3.1 of the plugin.
308 313
309 314
310 315
311 Accessing the content of a new instance 316 Accessing the content of a new instance
312 ....................................... 317 .......................................