Mercurial > hg > orthanc-book
annotate 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 |
rev | line source |
---|---|
343
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1 .. _python-plugin: |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
3 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
4 Python plugin for Orthanc |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
5 ========================= |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
6 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
7 .. contents:: |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
8 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
9 Work-in-progress. |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
10 |
349 | 11 Being a plugin, the Python API has access to more features than |
350 | 12 :ref:`Lua scripts <lua>`. |
349 | 13 |
345 | 14 The Python API is automatically generated from the `Orthanc plugin SDK |
15 in C | |
16 <https://hg.orthanc-server.com/orthanc/file/Orthanc-1.5.7/Plugins/Include/orthanc/OrthancCPlugin.h>`__ | |
17 using the `Clang <https://en.wikipedia.org/wiki/Clang>`__ compiler | |
18 front-end. The coverage of the C SDK is about 75% (105 functions are | |
19 automatically wrapped in Python out of a total of 139 functions in C). | |
343
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
20 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
21 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
22 Samples |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
23 ------- |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
24 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 Extending the REST API |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
26 ...................... |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
27 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 .. highlight:: python |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
29 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
30 Here is a basic Python script that registers two new routes in the |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
31 REST API:: |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
32 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
33 import orthanc |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
34 import pprint |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
35 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
36 def OnRest(output, uri, **request): |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
37 pprint.pprint(request) |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
38 print('Accessing uri: %s' % uri) |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
39 output.AnswerBuffer('ok\n', 'text/plain') |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
40 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
41 orthanc.RegisterRestCallback('/(to)(t)o', OnRest) |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
42 orthanc.RegisterRestCallback('/tata', OnRest) |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
43 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
44 .. highlight:: json |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
45 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
46 Here is the associated minimal configuration file for Orthanc |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
47 (provided the Python script is saved as ``rest.py``):: |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
48 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
49 { |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
50 "Plugins" : [ "." ], |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
51 "PythonScript" : "rest.py", |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
52 "PythonVerbose" : false |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
53 } |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
54 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
55 .. highlight:: bash |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
56 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
57 The route can then be accessed as:: |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
58 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
59 $ curl http://localhost:8042/toto |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
60 ok |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
61 |
345 | 62 |
63 Listening to changes | |
64 .................... | |
65 | |
346
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
66 .. highlight:: python |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
67 |
345 | 68 This sample uploads a DICOM file as soon as Orthanc is started:: |
69 | |
70 import orthanc | |
71 | |
72 def OnChange(changeType, level, resource): | |
73 if changeType == orthanc.ChangeType.ORTHANC_STARTED: | |
74 print('Started') | |
75 | |
76 with open('/tmp/sample.dcm', 'rb') as f: | |
77 orthanc.RestApiPost('/instances', f.read()) | |
78 | |
79 elif changeType == orthanc.ChangeType.ORTHANC_STOPPED: | |
80 print('Stopped') | |
81 | |
82 elif changeType == orthanc.ChangeType.NEW_INSTANCE: | |
83 print('A new instance was uploaded: %s' % resource) | |
84 | |
85 orthanc.RegisterOnChangeCallback(OnChange) | |
86 | |
87 | |
88 Accessing the content of a new instance | |
346
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
89 ....................................... |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
90 |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
91 .. highlight:: python |
345 | 92 |
93 :: | |
94 | |
95 import orthanc | |
96 import json | |
97 import pprint | |
98 | |
99 def OnStoredInstance(dicom, instanceId): | |
100 print('Received instance %s of size %d (transfer syntax %s, SOP class UID %s)' % ( | |
101 instanceId, dicom.GetInstanceSize(), | |
102 dicom.GetInstanceMetadata('TransferSyntax'), | |
103 dicom.GetInstanceMetadata('SopClassUid'))) | |
104 | |
105 # Print the origin information | |
106 if dicom.GetInstanceOrigin() == orthanc.InstanceOrigin.DICOM_PROTOCOL: | |
107 print('This instance was received through the DICOM protocol') | |
108 elif dicom.GetInstanceOrigin() == orthanc.InstanceOrigin.REST_API: | |
109 print('This instance was received through the REST API') | |
110 | |
111 # Print the DICOM tags | |
112 pprint.pprint(json.loads(dicom.GetInstanceSimplifiedJson())) | |
113 | |
114 orthanc.RegisterOnStoredInstanceCallback(OnStoredInstance) | |
346
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
115 |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
116 |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
117 Calling pydicom |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
118 ............... |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
119 |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
120 .. highlight:: python |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
121 |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
122 Here is a sample Python plugin that registers a REST callback to dump |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
123 the content of the dataset of one given DICOM instance stored in |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
124 Orthanc, using `pydicom <https://pydicom.github.io/>`__:: |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
125 |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
126 import io |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
127 import orthanc |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
128 import pydicom |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
129 |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
130 def DecodeInstance(output, uri, **request): |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
131 if request['method'] == 'GET': |
347 | 132 # Retrieve the instance ID from the regular expression (*) |
346
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
133 instanceId = request['groups'][0] |
347 | 134 # Get the content of the DICOM file |
346
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
135 f = orthanc.GetDicomForInstance(instanceId) |
347 | 136 # Parse it using pydicom |
346
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
137 dicom = pydicom.dcmread(io.BytesIO(f)) |
347 | 138 # Return a string representation the dataset to the caller |
346
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
139 output.AnswerBuffer(str(dicom), 'text/plain') |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
140 else: |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
141 output.SendMethodNotAllowed('GET') |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
142 |
347 | 143 orthanc.RegisterRestCallback('/pydicom/(.*)', DecodeInstance) # (*) |
346
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
144 |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
145 .. highlight:: bash |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
146 |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
147 This can be called as follows:: |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
148 |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
149 $ curl http://localhost:8042/pydicom/19816330-cb02e1cf-df3a8fe8-bf510623-ccefe9f5 |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
150 |
347 | 151 |
152 Auto-routing studies | |
153 .................... | |
154 | |
155 .. highlight:: python | |
156 | |
157 Here is a sample Python plugin that routes any :ref:`stable study | |
158 <lua-callbacks>` to a modality named ``samples`` (as declared in the | |
159 ``DicomModalities`` configuration option):: | |
160 | |
161 import orthanc | |
162 | |
163 def OnChange(changeType, level, resourceId): | |
164 if changeType == orthanc.ChangeType.STABLE_STUDY: | |
165 print('Stable study: %s' % resourceId) | |
166 orthanc.RestApiPost('/modalities/sample/store', resourceId) | |
167 | |
168 orthanc.RegisterOnChangeCallback(OnChange) | |
348 | 169 |
170 | |
352 | 171 Rendering a thumbnail using PIL/Pillow |
172 ...................................... | |
348 | 173 |
174 .. highlight:: python | |
175 | |
176 :: | |
177 | |
178 from PIL import Image | |
179 import io | |
180 import orthanc | |
181 | |
182 def DecodeInstance(output, uri, **request): | |
183 if request['method'] == 'GET': | |
184 # Retrieve the instance ID from the regular expression (*) | |
185 instanceId = request['groups'][0] | |
186 | |
187 # Render the instance, then open it in Python using PIL/Pillow | |
188 png = orthanc.RestApiGet('/instances/%s/rendered' % instanceId) | |
189 image = Image.open(io.BytesIO(png)) | |
190 | |
191 # Downsize the image as a 64x64 thumbnail | |
192 image.thumbnail((64, 64), Image.ANTIALIAS) | |
193 | |
194 # Save the thumbnail as JPEG, then send the buffer to the caller | |
195 jpeg = io.BytesIO() | |
196 image.save(jpeg, format = "JPEG", quality = 80) | |
197 jpeg.seek(0) | |
198 output.AnswerBuffer(jpeg.read(), 'text/plain') | |
199 | |
200 else: | |
201 output.SendMethodNotAllowed('GET') | |
202 | |
203 orthanc.RegisterRestCallback('/pydicom/(.*)', DecodeInstance) # (*) | |
351 | 204 |
205 | |
206 Performance and concurrency | |
207 --------------------------- | |
208 | |
209 .. highlight:: python | |
210 | |
211 Let us consider the following sample Python script that makes a | |
212 CPU-intensive computation on a REST callback:: | |
213 | |
214 import math | |
215 import orthanc | |
216 import time | |
217 | |
218 # CPU-intensive computation taking about 4 seconds | |
219 def SlowComputation(): | |
220 start = time.time() | |
221 for i in range(1000): | |
222 for j in range(30000): | |
223 math.sqrt(float(j)) | |
224 end = time.time() | |
225 duration = (end - start) | |
226 return 'computation done in %.03f seconds\n' % duration | |
227 | |
228 def OnRest(output, uri, **request): | |
229 answer = SlowComputation() | |
230 output.AnswerBuffer(answer, 'text/plain') | |
231 | |
232 orthanc.RegisterRestCallback('/computation', OnRest) | |
233 | |
234 | |
235 .. highlight:: text | |
236 | |
237 Calling this REST route from the command-line returns the time that is | |
238 needed to compute 30 million times a squared root on your CPU:: | |
239 | |
240 $ curl http://localhost:8042/computation | |
241 computation done in 4.208 seconds | |
242 | |
243 Now, let us call this route three times concurrently (we use bash):: | |
244 | |
245 $ (curl http://localhost:8042/computation & curl http://localhost:8042/computation & curl http://localhost:8042/computation ) | |
246 computation done in 11.262 seconds | |
247 computation done in 12.457 seconds | |
248 computation done in 13.360 seconds | |
249 | |
250 As can be seen, the computation time has tripled. This means that the | |
251 computations were not distributed across the available CPU cores. | |
252 This might seem surprising, as Orthanc is a threaded server (in | |
253 Orthanc, a pool of C++ threads serves concurrent requests). | |
254 | |
255 The explanation is that the Python interpreter (`CPython | |
256 <https://en.wikipedia.org/wiki/CPython>`__ actually) is built on the | |
257 top of a so-called `Global Interpreter Lock (GIL) | |
258 <https://en.wikipedia.org/wiki/Global_interpreter_lock>`__. The GIL is | |
259 basically a mutex that protects all the calls to the Python | |
260 interpreter. If multiple C++ threads from Orthanc call a Python | |
261 callback, only one can proceed at any given time. | |
262 | |
263 .. highlight:: python | |
264 | |
265 The solution is to use the `multiprocessing primitives | |
266 <https://docs.python.org/3/library/multiprocessing.html>`__ of Python. | |
267 The "master" Python interpreter that is initially started by the | |
268 Orthanc plugin, can start several `children processes | |
269 <https://en.wikipedia.org/wiki/Process_(computing)>`__, each of these | |
270 processes running a separate Python interpreter. This allows to | |
271 offload intensive computations from the "master" Python interpreter of | |
272 Orthanc onto those "slave" interpreters. The ``multiprocessing`` | |
273 library is actually quite straightforward to use:: | |
274 | |
275 import math | |
276 import multiprocessing | |
277 import orthanc | |
278 import signal | |
279 import time | |
280 | |
281 # CPU-intensive computation taking about 4 seconds | |
282 # (same code as above) | |
283 def SlowComputation(): | |
284 start = time.time() | |
285 for i in range(1000): | |
286 for j in range(30000): | |
287 math.sqrt(float(j)) | |
288 end = time.time() | |
289 duration = (end - start) | |
290 return 'computation done in %.03f seconds\n' % duration | |
291 | |
292 # Ignore CTRL+C in the slave processes | |
293 def Initializer(): | |
294 signal.signal(signal.SIGINT, signal.SIG_IGN) | |
295 | |
296 # Create a pool of 4 slave Python interpreters | |
297 POOL = multiprocessing.Pool(4, initializer = Initializer) | |
298 | |
299 def OnRest(output, uri, **request): | |
300 # Offload the call to "SlowComputation" onto one slave process. | |
301 # The GIL is unlocked until the slave sends its answer back. | |
302 answer = POOL.apply(SlowComputation) | |
303 output.AnswerBuffer(answer, 'text/plain') | |
304 | |
305 orthanc.RegisterRestCallback('/computation', OnRest) | |
306 | |
307 .. highlight:: text | |
308 | |
309 Here is now the result of calling this route three times concurrently:: | |
310 | |
311 $ (curl http://localhost:8042/computation & curl http://localhost:8042/computation & curl http://localhost:8042/computation ) | |
312 computation done in 4.211 seconds | |
313 computation done in 4.215 seconds | |
314 computation done in 4.225 seconds | |
315 | |
316 As can be seen, the calls to the Python computation now fully run in | |
317 parallel (the time is cut down from 12 seconds to 4 seconds, the same | |
318 as for one isolated request). | |
319 | |
320 Note also how the ``multiprocessing`` library allows to make a fine | |
321 control over the computational resources that are available to the | |
322 Python script: The number of "slave" interpreters can be easily | |
323 changed in the constructor of the ``multiprocessing.Pool`` object, and | |
324 are fully independent of the threads used by the Orthanc server. | |
325 | |
352 | 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 | |
351 | 351 Obviously, an in-depth discussion about the ``multiprocessing`` |
352 library is out of the scope of this document. There are many | |
353 references available on Internet. Also, note that ``multithreading`` | |
354 is not useful here, as Python multithreading is also limited by the | |
355 GIL, and is more targeted at dealing with costly I/O operations. |