Mercurial > hg > orthanc-book
comparison Sphinx/source/plugins/python/multiprocessing-2.py @ 702:6e02cd89eb6a
moving python samples in separate files
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 11 Jun 2021 09:38:15 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
701:f093160dd7f4 | 702:6e02cd89eb6a |
---|---|
1 import math | |
2 import multiprocessing | |
3 import orthanc | |
4 import signal | |
5 import time | |
6 | |
7 # CPU-intensive computation taking about 4 seconds | |
8 # (same code as above) | |
9 def SlowComputation(): | |
10 start = time.time() | |
11 for i in range(1000): | |
12 for j in range(30000): | |
13 math.sqrt(float(j)) | |
14 end = time.time() | |
15 duration = (end - start) | |
16 return 'computation done in %.03f seconds\n' % duration | |
17 | |
18 # Ignore CTRL+C in the slave processes | |
19 def Initializer(): | |
20 signal.signal(signal.SIGINT, signal.SIG_IGN) | |
21 | |
22 # Create a pool of 4 slave Python interpreters | |
23 POOL = multiprocessing.Pool(4, initializer = Initializer) | |
24 | |
25 def OnRest(output, uri, **request): | |
26 # Offload the call to "SlowComputation" onto one slave process. | |
27 # The GIL is unlocked until the slave sends its answer back. | |
28 answer = POOL.apply(SlowComputation) | |
29 output.AnswerBuffer(answer, 'text/plain') | |
30 | |
31 orthanc.RegisterRestCallback('/computation', OnRest) |