comparison Sphinx/source/plugins/python.rst @ 352:6258b2c14e56

cont
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 28 Mar 2020 14:29:06 +0100
parents e2863083fa30
children 0122c668f4ec
comparison
equal deleted inserted replaced
351:e2863083fa30 352:6258b2c14e56
166 orthanc.RestApiPost('/modalities/sample/store', resourceId) 166 orthanc.RestApiPost('/modalities/sample/store', resourceId)
167 167
168 orthanc.RegisterOnChangeCallback(OnChange) 168 orthanc.RegisterOnChangeCallback(OnChange)
169 169
170 170
171 Render a thumbnail using PIL/Pillow 171 Rendering a thumbnail using PIL/Pillow
172 ................................... 172 ......................................
173 173
174 .. highlight:: python 174 .. highlight:: python
175 175
176 :: 176 ::
177 177
321 control over the computational resources that are available to the 321 control over the computational resources that are available to the
322 Python script: The number of "slave" interpreters can be easily 322 Python script: The number of "slave" interpreters can be easily
323 changed in the constructor of the ``multiprocessing.Pool`` object, and 323 changed in the constructor of the ``multiprocessing.Pool`` object, and
324 are fully independent of the threads used by the Orthanc server. 324 are fully independent of the threads used by the Orthanc server.
325 325
326 .. highlight:: python
327
328 Very importantly, pay attention to the fact that only the "master"
329 Python interpreter has access to the Orthanc SDK. For instance, here
330 is how you would parse a DICOM file in a slave process::
331
332 import pydicom
333 import io
334
335 def OffloadedDicomParsing(dicom):
336 # No access to the "orthanc" library here, as we are in the slave process
337 dataset = pydicom.dcmread(io.BytesIO(dicom))
338 return str(dataset)
339
340 def OnRest(output, uri, **request):
341 # The call to "orthanc.RestApiGet()" is only possible in the master process
342 dicom = orthanc.RestApiGet('/instances/19816330-cb02e1cf-df3a8fe8-bf510623-ccefe9f5/file')
343 answer = POOL.apply(OffloadedDicomParsing, args = (dicom, ))
344 output.AnswerBuffer(answer, 'text/plain')
345
346 Communication primitives such as ``multiprocessing.Queue`` are
347 available to exchange messages from the "slave" Python interpreters to
348 the "master" Python interpreter if further calls to the Orthanc SDK
349 are required.
350
326 Obviously, an in-depth discussion about the ``multiprocessing`` 351 Obviously, an in-depth discussion about the ``multiprocessing``
327 library is out of the scope of this document. There are many 352 library is out of the scope of this document. There are many
328 references available on Internet. Also, note that ``multithreading`` 353 references available on Internet. Also, note that ``multithreading``
329 is not useful here, as Python multithreading is also limited by the 354 is not useful here, as Python multithreading is also limited by the
330 GIL, and is more targeted at dealing with costly I/O operations. 355 GIL, and is more targeted at dealing with costly I/O operations.