Belle II Software
development
calculation_queue.py
1
#!/usr/bin/env python3
2
3
10
11
from
multiprocessing
import
Queue
12
13
try
:
14
from
queue
import
Empty
15
except
ImportError:
16
from
Queue
import
Empty
17
18
19
class
CalculationQueue
:
20
"""
21
This class is a wrapper around a multiprocessing.Queue
22
23
It can be used to send and receive values from the modules while processing the calculation.
24
You can use it to save - for example - filepaths of outputfiles that you create on the fly.
25
The added items are all of the type CalculationQueueItem.
26
The CalculationQueue can be used as a dict. After the termination of the underlying process
27
you can access the different entries by their names you gave them when putting them on the queue.
28
"""
29
30
def
__init__
(self):
31
"""
32
Create a queue.
33
"""
34
35
self.
queue
= Queue()
36
37
self.
results
= dict()
38
39
def
put
(self, name, item, **kwargs):
40
"""
41
Put an item on the queue with the given name. Please keep that adding two items with the same name
42
overrides one of them!
43
"""
44
self.
queue
.
put
(
CalculationQueueItem
(name, item), block=
True
, **kwargs)
45
46
def
fill_results
(self):
47
"""
48
Fill the internal dict with the information of the queue.
49
Do not call this on your own.
50
Do only call this when the underlying process has ended.
51
"""
52
while
True
:
53
try
:
54
result = self.
queue
.get_nowait()
55
self.
results
.update({result.name: result.item})
56
except
Empty:
57
return
58
59
def
get
(self, name):
60
"""
61
Return the item with the given name or an Exception when it is not found.
62
Do not call this on your own..
63
"""
64
self.
fill_results
()
65
return
self.
results
[name]
66
67
def
get_keys
(self):
68
"""
69
Return all possible names of items saved in this queue.
70
Do not call this on your own.
71
"""
72
self.
fill_results
()
73
return
list(self.
results
.keys())
74
75
76
class
CalculationQueueItem
:
77
"""
78
A placeholder for a tuple string, object.
79
Do not create them by yourself.
80
"""
81
82
def
__init__
(self, name, item):
83
"""
84
Create a new queue item
85
"""
86
87
self.
name
= name
88
89
self.
item
= item
90
91
def
__eq__
(self, other):
92
"""
93
Equality operator needed for tests.
94
"""
95
return
other.name == self.
name
and
other.item == self.
item
hep_ipython_tools.calculation_queue.CalculationQueueItem
Definition
calculation_queue.py:76
hep_ipython_tools.calculation_queue.CalculationQueueItem.__init__
__init__(self, name, item)
Definition
calculation_queue.py:82
hep_ipython_tools.calculation_queue.CalculationQueueItem.item
item
Item to store.
Definition
calculation_queue.py:89
hep_ipython_tools.calculation_queue.CalculationQueueItem.__eq__
__eq__(self, other)
Definition
calculation_queue.py:91
hep_ipython_tools.calculation_queue.CalculationQueueItem.name
name
Name of the item.
Definition
calculation_queue.py:87
hep_ipython_tools.calculation_queue.CalculationQueue
Definition
calculation_queue.py:19
hep_ipython_tools.calculation_queue.CalculationQueue.put
put(self, name, item, **kwargs)
Definition
calculation_queue.py:39
hep_ipython_tools.calculation_queue.CalculationQueue.results
results
The results to be filled in the end.
Definition
calculation_queue.py:37
hep_ipython_tools.calculation_queue.CalculationQueue.get
get(self, name)
Definition
calculation_queue.py:59
hep_ipython_tools.calculation_queue.CalculationQueue.get_keys
get_keys(self)
Definition
calculation_queue.py:67
hep_ipython_tools.calculation_queue.CalculationQueue.__init__
__init__(self)
Definition
calculation_queue.py:30
hep_ipython_tools.calculation_queue.CalculationQueue.fill_results
fill_results(self)
Definition
calculation_queue.py:46
hep_ipython_tools.calculation_queue.CalculationQueue.queue
queue
The multiprocessing queue to handle.
Definition
calculation_queue.py:35
framework
scripts
hep_ipython_tools
calculation_queue.py
Generated on Mon Sep 1 2025 02:51:51 for Belle II Software by
1.13.2