Mercurial > hg > orthanc
annotate Resources/Samples/Python/HighPerformanceAutoRouting.py @ 2043:35ccba7adae9
Toolbox::UriEncode
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 23 Jun 2016 10:08:27 +0200 |
parents | 65b1ce7cb84f |
children | a3a65de1840f |
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 |
747
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
7 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
8 # 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
|
9 # 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
|
10 # 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
|
11 # 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
|
12 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
13 # 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
|
14 # 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
|
15 # 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
|
16 # General Public License for more details. |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
17 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
18 # 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
|
19 # 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
|
20 |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
21 |
404 | 22 |
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
|
23 URL = 'http://127.0.0.1:8042' |
404 | 24 TARGET = 'sample' |
25 | |
26 | |
27 # | |
28 # This sample code shows how to setup a simple, high-performance DICOM | |
29 # auto-routing. All the DICOM instances that arrive inside Orthanc | |
30 # will be sent to a remote modality. A producer-consumer pattern is | |
31 # used. The target modality is specified by the TARGET variable above: | |
32 # It must match an entry in the Orthanc configuration file inside the | |
33 # "DicomModalities" section. | |
34 # | |
35 # NOTE: This sample only works with Orthanc >= 0.5.2. Make sure that | |
36 # Orthanc was built with "-DCMAKE_BUILD_TYPE=Release" to get the best | |
37 # performance. | |
38 # | |
39 | |
40 import Queue | |
41 import sys | |
42 import time | |
43 import threading | |
44 | |
45 import RestToolbox | |
46 | |
47 | |
48 # | |
49 # Queue that is shared between the producer and the consumer | |
50 # threads. It holds the instances that are still to be sent. | |
51 # | |
52 | |
53 queue = Queue.Queue() | |
54 | |
55 | |
56 # | |
57 # The producer thread. It monitors the arrival of new instances into | |
58 # Orthanc, and pushes their ID into the shared queue. This code is | |
59 # based upon the "ChangesLoop.py" sample code. | |
60 # | |
61 | |
62 def Producer(queue): | |
63 current = 0 | |
64 | |
65 while True: | |
66 r = RestToolbox.DoGet(URL + '/changes', { | |
67 'since' : current, | |
68 'limit' : 4 # Retrieve at most 4 changes at once | |
69 }) | |
70 | |
71 for change in r['Changes']: | |
72 # We are only interested interested in the arrival of new instances | |
73 if change['ChangeType'] == 'NewInstance': | |
74 queue.put(change['ID']) | |
75 | |
76 current = r['Last'] | |
77 | |
78 if r['Done']: | |
79 time.sleep(1) | |
80 | |
81 | |
82 # | |
83 # The consumer thread. It continuously reads the instances from the | |
84 # queue, and send them to the remote modality. Each time a packet of | |
85 # instances is sent, a single DICOM connexion is used, hence improving | |
86 # the performance. | |
87 # | |
88 | |
89 def Consumer(queue): | |
90 TIMEOUT = 0.1 | |
91 | |
92 while True: | |
93 instances = [] | |
94 | |
95 while True: | |
96 try: | |
97 # Block for a while, waiting for the arrival of a new | |
98 # instance | |
99 instance = queue.get(True, TIMEOUT) | |
100 | |
101 # A new instance has arrived: Record its ID | |
102 instances.append(instance) | |
103 queue.task_done() | |
104 | |
105 except Queue.Empty: | |
106 # Timeout: No more data was received | |
107 break | |
108 | |
109 if len(instances) > 0: | |
1184 | 110 print('Sending a packet of %d instances' % len(instances)) |
404 | 111 start = time.time() |
112 | |
113 # Send all the instances with a single DICOM connexion | |
114 RestToolbox.DoPost('%s/modalities/sample/store' % URL, instances) | |
115 | |
116 # Remove all the instances from Orthanc | |
117 for instance in instances: | |
118 RestToolbox.DoDelete('%s/instances/%s' % (URL, instance)) | |
119 | |
413
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
404
diff
changeset
|
120 # Clear the log of the exported instances (to prevent the |
1143 | 121 # SQLite database from growing indefinitely). More simply, |
122 # you could also set the "LogExportedResources" option to | |
1144 | 123 # "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
|
124 RestToolbox.DoDelete('%s/exports' % URL) |
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
404
diff
changeset
|
125 |
404 | 126 end = time.time() |
1184 | 127 print('The packet of %d instances has been sent in %d seconds' % (len(instances), end - start)) |
404 | 128 |
129 | |
130 # | |
131 # Thread to display the progress | |
132 # | |
133 | |
134 def PrintProgress(queue): | |
135 while True: | |
1184 | 136 print('Current queue size: %d' % (queue.qsize())) |
404 | 137 time.sleep(1) |
138 | |
139 | |
140 # | |
141 # Start the various threads | |
142 # | |
143 | |
144 progress = threading.Thread(None, PrintProgress, None, (queue, )) | |
145 progress.daemon = True | |
146 progress.start() | |
147 | |
148 producer = threading.Thread(None, Producer, None, (queue, )) | |
149 producer.daemon = True | |
150 producer.start() | |
151 | |
152 consumer = threading.Thread(None, Consumer, None, (queue, )) | |
153 consumer.daemon = True | |
154 consumer.start() | |
155 | |
156 | |
157 # | |
158 # Active waiting for Ctrl-C | |
159 # | |
160 | |
161 while True: | |
162 time.sleep(0.1) |