Belle II Software  release-05-02-19
calculation_process.py
1 import os
2 from multiprocessing import Process, Pipe
3 
4 
5 class CalculationProcess(Process):
6  """
7  Abstract base class for doing the real processing of a calculation. Is used by the handler to
8  process the calculation you requested. Overload the start_process and prepare method to implement your calculation
9  and maybe also the __init__ method if you need to store more information on your process (like what to calculate).
10 
11  See ipython_handler_basf2/calculation_process.py for an example implementation.
12  """
13 
14  def __init__(self, result_queue, log_file_name, parameters):
15  """
16  Create a new calculation process instance. The parameters here are the absolute standard. You probably have to redefine the
17  constructor in your own class.
18  """
19  if result_queue is None:
20  raise ValueError("Invalid result_queue")
21 
22 
23  self.already_run = False
24 
25 
26  self.log_file_name = log_file_name
27 
28 
29  self.log_file_content = None
30 
31 
32  self.result_queue = result_queue
33 
34 
35  self.parameters = parameters
36 
37 
38  self.progress_queue_local, self.progress_queue_remote = Pipe()
39 
40 
41  self.is_valid = True
42 
43  # Prepare the environment.
44  self.prepare()
45 
46  # Call the constructor of the base class.
47  Process.__init__(self, target=self.start_process)
48 
49  def save_log(self):
50  """
51  Delete the log file and copy its content to the class.
52  """
53  if self.log_file_content is None:
54  self.log_file_content = open(self.log_file_name).read()
55  os.unlink(self.log_file_name)
56  self.log_file_name = None
57 
58  def get_log(self):
59  """
60  Return the log file content.
61  Use the methods of the Calculation for a better handling.
62  """
63  if self.is_alive():
64  return open(self.log_file_name).read()
65  else:
66  self.save_log()
67  return self.log_file_content
68 
69  def get(self, name):
70  """
71  Return an item from the result queue. Only gives a result if the calculation has finished.
72  Use the Calculation for a better handling.
73  """
74  if not self.is_alive():
75  return self.result_queue.get(name)
76 
77  def get_keys(self):
78  """
79  Return the names of all item from the result queue. Only gives a result if the calculation has finished.
80  Use the Calculation for a better handling.
81  """
82  if not self.is_alive():
83  return self.result_queue.get_keys()
84 
85  def start_process(self):
86  """
87  The function given to the process to start the calculation.
88  Do not call by yourself.
89  Resets the logging system, logs onto console and a file and sets the queues
90  (the result queue and the process queue) correctly.
91  """
92  pass
93 
94  def prepare(self):
95  """
96  Overload this function if you need to process some preparations
97  before doing the real calculation.
98  """
99  pass
hep_ipython_tools.calculation_process.CalculationProcess.result_queue
result_queue
Result queue as a reference.
Definition: calculation_process.py:32
hep_ipython_tools.calculation_process.CalculationProcess.get
def get(self, name)
Definition: calculation_process.py:69
hep_ipython_tools.calculation_process.CalculationProcess.prepare
def prepare(self)
Definition: calculation_process.py:94
hep_ipython_tools.calculation_process.CalculationProcess.log_file_name
log_file_name
Name of the log file to use.
Definition: calculation_process.py:26
hep_ipython_tools.calculation_process.CalculationProcess.log_file_content
log_file_content
Saved log file content after the run.
Definition: calculation_process.py:29
hep_ipython_tools.calculation_process.CalculationProcess.__init__
def __init__(self, result_queue, log_file_name, parameters)
Definition: calculation_process.py:14
hep_ipython_tools.calculation_process.CalculationProcess.start_process
def start_process(self)
Definition: calculation_process.py:85
hep_ipython_tools.calculation_process.CalculationProcess.is_valid
is_valid
Set to false, if you do not want this process to show up in the process bar calculations.
Definition: calculation_process.py:41
hep_ipython_tools.calculation_process.CalculationProcess.get_log
def get_log(self)
Definition: calculation_process.py:58
hep_ipython_tools.calculation_process.CalculationProcess.already_run
already_run
True if already started/run.
Definition: calculation_process.py:23
hep_ipython_tools.calculation_process.CalculationProcess.get_keys
def get_keys(self)
Definition: calculation_process.py:77
hep_ipython_tools.calculation_process.CalculationProcess.parameters
parameters
Parameters in process_parameter_space.
Definition: calculation_process.py:35
hep_ipython_tools.calculation_process.CalculationProcess.progress_queue_remote
progress_queue_remote
Create the queue for the progress python module.
Definition: calculation_process.py:38
hep_ipython_tools.calculation_process.CalculationProcess.save_log
def save_log(self)
Definition: calculation_process.py:49
hep_ipython_tools.calculation_process.CalculationProcess
Definition: calculation_process.py:5