annotate Sphinx/source/plugins/python/multiprocessing-4.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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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)