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