Mercurial > hg > orthanc-book
annotate Sphinx/source/plugins/python/multiprocessing-1.py @ 789:2f70f622059b
added odbc tip
author | Alain Mazy <am@osimis.io> |
---|---|
date | Wed, 24 Nov 2021 15:30:29 +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 math |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2 import orthanc |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
3 import time |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
4 |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
5 # CPU-intensive computation taking about 4 seconds |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
6 def SlowComputation(): |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
7 start = time.time() |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
8 for i in range(1000): |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
9 for j in range(30000): |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
10 math.sqrt(float(j)) |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
11 end = time.time() |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
12 duration = (end - start) |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
13 return 'computation done in %.03f seconds\n' % duration |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
14 |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
15 def OnRest(output, uri, **request): |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
16 answer = SlowComputation() |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
17 output.AnswerBuffer(answer, 'text/plain') |
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 orthanc.RegisterRestCallback('/computation', OnRest) |