annotate Resources/Samples/Python/HighPerformanceAutoRouting.py @ 445:7816aaa5db17

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