Mercurial > hg > orthanc
annotate OrthancServer/Resources/Samples/Python/HighPerformanceAutoRouting.py @ 4273:0034f855c023
tuning log categories from command-line, and binary compat with orthanc framework 1.7.2
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 03 Nov 2020 12:24:50 +0100 |
parents | d25f4c0fa160 |
children | d9473bd5ed43 |
rev | line source |
---|---|
404 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
747
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
4 # Orthanc - A Lightweight, RESTful DICOM Store |
1900 | 5 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1184
diff
changeset
|
6 # Department, University Hospital of Liege, Belgium |
3640
94f4a18a79cc
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3340
diff
changeset
|
7 # Copyright (C) 2017-2020 Osimis S.A., Belgium |
747
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
8 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
9 # This program is free software: you can redistribute it and/or |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
10 # modify it under the terms of the GNU General Public License as |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
11 # published by the Free Software Foundation, either version 3 of the |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
12 # License, or (at your option) any later version. |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
13 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
14 # This program is distributed in the hope that it will be useful, but |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
15 # WITHOUT ANY WARRANTY; without even the implied warranty of |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
17 # General Public License for more details. |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
18 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
19 # You should have received a copy of the GNU General Public License |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
20 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
21 |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
22 |
404 | 23 |
2032
65b1ce7cb84f
Replaced "localhost" by "127.0.0.1", as it might impact performance on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
24 URL = 'http://127.0.0.1:8042' |
404 | 25 TARGET = 'sample' |
26 | |
27 | |
28 # | |
29 # This sample code shows how to setup a simple, high-performance DICOM | |
30 # auto-routing. All the DICOM instances that arrive inside Orthanc | |
31 # will be sent to a remote modality. A producer-consumer pattern is | |
32 # used. The target modality is specified by the TARGET variable above: | |
33 # It must match an entry in the Orthanc configuration file inside the | |
34 # "DicomModalities" section. | |
35 # | |
36 # NOTE: This sample only works with Orthanc >= 0.5.2. Make sure that | |
37 # Orthanc was built with "-DCMAKE_BUILD_TYPE=Release" to get the best | |
38 # performance. | |
39 # | |
40 | |
41 import Queue | |
42 import sys | |
43 import time | |
44 import threading | |
45 | |
46 import RestToolbox | |
47 | |
48 | |
49 # | |
50 # Queue that is shared between the producer and the consumer | |
51 # threads. It holds the instances that are still to be sent. | |
52 # | |
53 | |
54 queue = Queue.Queue() | |
55 | |
56 | |
57 # | |
58 # The producer thread. It monitors the arrival of new instances into | |
59 # Orthanc, and pushes their ID into the shared queue. This code is | |
60 # based upon the "ChangesLoop.py" sample code. | |
61 # | |
62 | |
63 def Producer(queue): | |
64 current = 0 | |
65 | |
66 while True: | |
67 r = RestToolbox.DoGet(URL + '/changes', { | |
68 'since' : current, | |
69 'limit' : 4 # Retrieve at most 4 changes at once | |
70 }) | |
71 | |
72 for change in r['Changes']: | |
3340 | 73 # We are only interested in the arrival of new instances |
404 | 74 if change['ChangeType'] == 'NewInstance': |
75 queue.put(change['ID']) | |
76 | |
77 current = r['Last'] | |
78 | |
79 if r['Done']: | |
80 time.sleep(1) | |
81 | |
82 | |
83 # | |
84 # The consumer thread. It continuously reads the instances from the | |
85 # queue, and send them to the remote modality. Each time a packet of | |
86 # instances is sent, a single DICOM connexion is used, hence improving | |
87 # the performance. | |
88 # | |
89 | |
90 def Consumer(queue): | |
91 TIMEOUT = 0.1 | |
92 | |
93 while True: | |
94 instances = [] | |
95 | |
96 while True: | |
97 try: | |
98 # Block for a while, waiting for the arrival of a new | |
99 # instance | |
100 instance = queue.get(True, TIMEOUT) | |
101 | |
102 # A new instance has arrived: Record its ID | |
103 instances.append(instance) | |
104 queue.task_done() | |
105 | |
106 except Queue.Empty: | |
107 # Timeout: No more data was received | |
108 break | |
109 | |
110 if len(instances) > 0: | |
1184 | 111 print('Sending a packet of %d instances' % len(instances)) |
404 | 112 start = time.time() |
113 | |
114 # Send all the instances with a single DICOM connexion | |
115 RestToolbox.DoPost('%s/modalities/sample/store' % URL, instances) | |
116 | |
117 # Remove all the instances from Orthanc | |
118 for instance in instances: | |
119 RestToolbox.DoDelete('%s/instances/%s' % (URL, instance)) | |
120 | |
413
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
404
diff
changeset
|
121 # Clear the log of the exported instances (to prevent the |
1143 | 122 # SQLite database from growing indefinitely). More simply, |
123 # you could also set the "LogExportedResources" option to | |
1144 | 124 # "false" in the configuration file since Orthanc 0.8.3. |
413
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
404
diff
changeset
|
125 RestToolbox.DoDelete('%s/exports' % URL) |
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
404
diff
changeset
|
126 |
404 | 127 end = time.time() |
1184 | 128 print('The packet of %d instances has been sent in %d seconds' % (len(instances), end - start)) |
404 | 129 |
130 | |
131 # | |
132 # Thread to display the progress | |
133 # | |
134 | |
135 def PrintProgress(queue): | |
136 while True: | |
1184 | 137 print('Current queue size: %d' % (queue.qsize())) |
404 | 138 time.sleep(1) |
139 | |
140 | |
141 # | |
142 # Start the various threads | |
143 # | |
144 | |
145 progress = threading.Thread(None, PrintProgress, None, (queue, )) | |
146 progress.daemon = True | |
147 progress.start() | |
148 | |
149 producer = threading.Thread(None, Producer, None, (queue, )) | |
150 producer.daemon = True | |
151 producer.start() | |
152 | |
153 consumer = threading.Thread(None, Consumer, None, (queue, )) | |
154 consumer.daemon = True | |
155 consumer.start() | |
156 | |
157 | |
158 # | |
159 # Active waiting for Ctrl-C | |
160 # | |
161 | |
162 while True: | |
163 time.sleep(0.1) |