ATLAS Offline Software
Loading...
Searching...
No Matches
repeating_timer.py
Go to the documentation of this file.
1# Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
2
3from AthenaCommon import Logging
4import datetime
5import threading
6
7
8logger = Logging.logging.getLogger("PowhegControl")
9
10
11class 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
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))
Recurring heartbeat that emits a message to console and to file.
__init__(self, interval, output_file=None)
Constructor.
__emit_heartbeat(self)
Output a heartbeat message.
Helper class to run repeating timing output in an additional thread.
readable_duration(duration)
Return duration in a human-readable form.
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:177
Definition run.py:1