ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Generators
PowhegControl
python
utility
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
..
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
42
class
HeartbeatTimer
(
RepeatingTimer
):
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))
python.utility.repeating_timer.HeartbeatTimer
Recurring heartbeat that emits a message to console and to file.
Definition
repeating_timer.py:42
python.utility.repeating_timer.HeartbeatTimer.__init__
__init__(self, interval, output_file=None)
Constructor.
Definition
repeating_timer.py:48
python.utility.repeating_timer.HeartbeatTimer.start_time
start_time
Definition
repeating_timer.py:54
python.utility.repeating_timer.HeartbeatTimer.__emit_heartbeat
__emit_heartbeat(self)
Output a heartbeat message.
Definition
repeating_timer.py:58
python.utility.repeating_timer.HeartbeatTimer.output_file
output_file
Definition
repeating_timer.py:55
python.utility.repeating_timer.RepeatingTimer
Helper class to run repeating timing output in an additional thread.
Definition
repeating_timer.py:11
python.utility.repeating_timer.RepeatingTimer.kwargs
kwargs
Definition
repeating_timer.py:24
python.utility.repeating_timer.RepeatingTimer.run
run(self)
Override Thread.run with desired behaviour.
Definition
repeating_timer.py:17
python.utility.repeating_timer.RepeatingTimer.readable_duration
readable_duration(duration)
Return duration in a human-readable form.
Definition
repeating_timer.py:27
python.utility.repeating_timer.RepeatingTimer.interval
interval
Definition
repeating_timer.py:20
python.utility.repeating_timer.RepeatingTimer.args
args
Definition
repeating_timer.py:24
split
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition
hcg.cxx:179
Generated on
for ATLAS Offline Software by
1.17.0