ATLAS Offline Software
repeating_timer.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
2 
3 from AthenaCommon import Logging
4 import datetime
5 import threading
6 
7 
8 logger = Logging.logging.getLogger("PowhegControl")
9 
10 
11 class RepeatingTimer(threading.Timer):
12  """! Helper class to run repeating timing output in an additional thread.
13 
14  @author http://stackoverflow.com/questions/5429577/
15  """
16 
17  def run(self):
18  """! Override Thread.run with desired behaviour."""
19  while True:
20  self.finished.wait(self.interval)
21  if self.finished.is_set():
22  return
23  else:
24  self.function(*self.args, **self.kwargs)
25 
26  @staticmethod
27  def readable_duration(duration):
28  """! Return duration in a human-readable form.
29 
30  @param duration Time interval to print.
31  """
32  if not isinstance(duration, datetime.timedelta):
33  duration = datetime.timedelta(seconds=duration)
34  h, m, s = str(duration).split(":")
35  if h != "0":
36  return "{}h {:>02}m {:>02}s".format(h, m, int(float(s)))
37  if m != "00":
38  return "{}m {:>02}s".format(int(float(m)), int(float(s)))
39  return "{:.2f}s".format(float(s))
40 
41 
43  """! Recurring heartbeat that emits a message to console and to file.
44 
45  @author James Robinson <james.robinson@cern.ch>
46  """
47 
48  def __init__(self, interval, output_file=None):
49  """! Constructor.
50 
51  @param interval Time interval between output messages in seconds.
52  @param output_file Log file to write to.
53  """
54  self.start_time = datetime.datetime.now()
55  self.output_file = output_file
56  super(HeartbeatTimer, self).__init__(interval, lambda: self.__emit_heartbeat())
57 
58  def __emit_heartbeat(self):
59  """! Output a heartbeat message."""
60  duration = RepeatingTimer.readable_duration(datetime.datetime.now() - self.start_time)
61  message = "Heartbeat: Powheg generation has been running for {} in total".format(duration)
62  logger.info(message)
63  if self.output_file is not None:
64  try:
65  with open(self.output_file, "w") as f:
66  f.write(message)
67  except IOError as detail:
68  logger.error("I/O error: {}".format(detail))
vtune_athena.format
format
Definition: vtune_athena.py:14
python.utility.repeating_timer.RepeatingTimer.readable_duration
def readable_duration(duration)
Return duration in a human-readable form.
Definition: repeating_timer.py:27
python.utility.repeating_timer.HeartbeatTimer.output_file
output_file
Definition: repeating_timer.py:55
python.utility.repeating_timer.RepeatingTimer.run
def run(self)
Override Thread.run with desired behaviour.
Definition: repeating_timer.py:17
python.utility.repeating_timer.HeartbeatTimer
Recurring heartbeat that emits a message to console and to file.
Definition: repeating_timer.py:42
Trk::open
@ open
Definition: BinningType.h:40
python.utility.repeating_timer.HeartbeatTimer.__emit_heartbeat
def __emit_heartbeat(self)
Output a heartbeat message.
Definition: repeating_timer.py:58
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
python.utility.repeating_timer.HeartbeatTimer.__init__
def __init__(self, interval, output_file=None)
Constructor.
Definition: repeating_timer.py:48
str
Definition: BTagTrackIpAccessor.cxx:11
python.utility.repeating_timer.HeartbeatTimer.start_time
start_time
Definition: repeating_timer.py:54
python.utility.repeating_timer.RepeatingTimer
Helper class to run repeating timing output in an additional thread.
Definition: repeating_timer.py:11
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
python.LArMinBiasAlgConfig.float
float
Definition: LArMinBiasAlgConfig.py:65