Mercurial > hg > orthanc
annotate Resources/Samples/Python/HighPerformanceAutoRouting.py @ 1073:01414536c930
complete DICOM conformance
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 29 Jul 2014 10:24:09 +0200 |
parents | 44382c8bcd15 |
children | 8bff277e6886 |
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 |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
5 # Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
6 # Belgium |
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 |
23 URL = 'http://localhost:8042' | |
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: | |
110 print 'Sending a packet of %d instances' % len(instances) | |
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 |
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
404
diff
changeset
|
121 # SQLite database from growing indefinitely) |
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
404
diff
changeset
|
122 RestToolbox.DoDelete('%s/exports' % URL) |
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
404
diff
changeset
|
123 |
404 | 124 end = time.time() |
125 print 'The packet of %d instances has been sent in %d seconds' % (len(instances), end - start) | |
126 | |
127 | |
128 # | |
129 # Thread to display the progress | |
130 # | |
131 | |
132 def PrintProgress(queue): | |
133 while True: | |
134 print 'Current queue size: %d' % (queue.qsize()) | |
135 time.sleep(1) | |
136 | |
137 | |
138 # | |
139 # Start the various threads | |
140 # | |
141 | |
142 progress = threading.Thread(None, PrintProgress, None, (queue, )) | |
143 progress.daemon = True | |
144 progress.start() | |
145 | |
146 producer = threading.Thread(None, Producer, None, (queue, )) | |
147 producer.daemon = True | |
148 producer.start() | |
149 | |
150 consumer = threading.Thread(None, Consumer, None, (queue, )) | |
151 consumer.daemon = True | |
152 consumer.start() | |
153 | |
154 | |
155 # | |
156 # Active waiting for Ctrl-C | |
157 # | |
158 | |
159 while True: | |
160 time.sleep(0.1) |