Belle II Software  release-08-01-10
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 
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 underlaying 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.queuequeue = Queue()
36 
37  self.resultsresults = 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.queuequeue.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.queuequeue.get_nowait()
55  self.resultsresults.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_resultsfill_results()
65  return self.resultsresults[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_resultsfill_results()
73  return list(self.resultsresults.keys())
74 
75 
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.namename = name
88 
89  self.itemitem = item
90 
91  def __eq__(self, other):
92  """
93  Equality operator needed for tests.
94  """
95  return other.name == self.namename and other.item == self.itemitem