Mercurial > hg > orthanc-book
annotate Sphinx/source/plugins/python/multiprocessing-4.py @ 1113:a588960a72e5 default tip
spelling
author | Alain Mazy <am@orthanc.team> |
---|---|
date | Mon, 28 Oct 2024 09:23:08 +0100 |
parents | 6e02cd89eb6a |
children |
rev | line source |
---|---|
702
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1 import json |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2 import multiprocessing |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
3 import orthanc |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
4 import requests |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
5 import signal |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
6 |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
7 TOKEN = orthanc.GenerateRestApiAuthorizationToken() |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
8 |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
9 def SlaveProcess(): |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
10 r = requests.get('http://localhost:8042/instances', |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
11 headers = { 'Authorization' : TOKEN }) |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
12 return json.dumps(r.json()) |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
13 |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
14 def Initializer(): |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
15 signal.signal(signal.SIGINT, signal.SIG_IGN) |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
16 |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
17 POOL = multiprocessing.Pool(4, initializer = Initializer) |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
18 |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
19 def OnRest(output, uri, **request): |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
20 answer = POOL.apply(SlaveProcess) |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
21 output.AnswerBuffer(answer, 'text/plain') |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
22 |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
23 orthanc.RegisterRestCallback('/computation', OnRest) |