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