Skip to content

Commit 57012e0

Browse files
committed
Add mitogen.core.now() and use it everywhere; closes #614.
1 parent 379dca9 commit 57012e0

20 files changed

+81
-58
lines changed

docs/api.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,14 @@ Fork Safety
603603
Utility Functions
604604
=================
605605

606+
.. currentmodule:: mitogen.core
607+
.. function:: now
608+
609+
A reference to :func:`time.time` on Python 2, or :func:`time.monotonic` on
610+
Python >3.3. We prefer :func:`time.monotonic` when available to ensure
611+
timers are not impacted by system clock changes.
612+
613+
606614
.. module:: mitogen.utils
607615

608616
A random assortment of utility functions useful on masters and children.

mitogen/core.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,10 @@ def to_text(o):
362362
return UnicodeType(o)
363363

364364

365+
# Documented in api.rst to work around Sphinx limitation.
366+
now = getattr(time, 'monotonic', time.time)
367+
368+
365369
# Python 2.4
366370
try:
367371
any
@@ -636,7 +640,7 @@ def _real_profile_hook(name, func, *args):
636640
return func(*args)
637641
finally:
638642
path = _profile_fmt % {
639-
'now': int(1e6 * time.time()),
643+
'now': int(1e6 * now()),
640644
'identity': name,
641645
'pid': os.getpid(),
642646
'ext': '%s'
@@ -3482,9 +3486,9 @@ def _broker_shutdown(self):
34823486
for _, (side, _) in self.poller.readers + self.poller.writers:
34833487
self._call(side.stream, side.stream.on_shutdown)
34843488

3485-
deadline = time.time() + self.shutdown_timeout
3486-
while self.keep_alive() and time.time() < deadline:
3487-
self._loop_once(max(0, deadline - time.time()))
3489+
deadline = now() + self.shutdown_timeout
3490+
while self.keep_alive() and now() < deadline:
3491+
self._loop_once(max(0, deadline - now()))
34883492

34893493
if self.keep_alive():
34903494
LOG.error('%r: pending work still existed %d seconds after '

mitogen/master.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -910,9 +910,9 @@ def _build_tuple(self, fullname):
910910

911911
if self.minify_safe_re.search(source):
912912
# If the module contains a magic marker, it's safe to minify.
913-
t0 = time.time()
913+
t0 = mitogen.core.now()
914914
source = mitogen.minify.minimize_source(source).encode('utf-8')
915-
self.minify_secs += time.time() - t0
915+
self.minify_secs += mitogen.core.now() - t0
916916

917917
if is_pkg:
918918
pkg_present = get_child_modules(path)
@@ -1001,11 +1001,11 @@ def _on_get_module(self, msg):
10011001
LOG.warning('_on_get_module(): dup request for %r from %r',
10021002
fullname, stream)
10031003

1004-
t0 = time.time()
1004+
t0 = mitogen.core.now()
10051005
try:
10061006
self._send_module_and_related(stream, fullname)
10071007
finally:
1008-
self.get_module_secs += time.time() - t0
1008+
self.get_module_secs += mitogen.core.now() - t0
10091009

10101010
def _send_forward_module(self, stream, context, fullname):
10111011
if stream.protocol.remote_id != context.context_id:

mitogen/parent.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ class TimerList(object):
633633
:meth:`expire`. The main user interface to :class:`TimerList` is
634634
:meth:`schedule`.
635635
"""
636-
_now = time.time
636+
_now = mitogen.core.now
637637

638638
def __init__(self):
639639
self._lst = []
@@ -1124,7 +1124,7 @@ def __init__(self, **kwargs):
11241124

11251125
def on_line_received(self, line):
11261126
self.logged_partial = None
1127-
self.logged_lines.append((time.time(), line))
1127+
self.logged_lines.append((mitogen.core.now(), line))
11281128
self.logged_lines[:] = self.logged_lines[-100:]
11291129
return super(LineLoggingProtocolMixin, self).on_line_received(line)
11301130

@@ -1134,7 +1134,7 @@ def on_partial_line_received(self, line):
11341134

11351135
def on_disconnect(self, broker):
11361136
if self.logged_partial:
1137-
self.logged_lines.append((time.time(), self.logged_partial))
1137+
self.logged_lines.append((mitogen.core.now(), self.logged_partial))
11381138
self.logged_partial = None
11391139
super(LineLoggingProtocolMixin, self).on_disconnect(broker)
11401140

@@ -1324,7 +1324,7 @@ def __init__(self, max_message_size, name=None, remote_name=None,
13241324
self.profiling = profiling
13251325
self.unidirectional = unidirectional
13261326
self.max_message_size = max_message_size
1327-
self.connect_deadline = time.time() + self.connect_timeout
1327+
self.connect_deadline = mitogen.core.now() + self.connect_timeout
13281328

13291329

13301330
class Connection(object):
@@ -1819,7 +1819,7 @@ def make_chain_id(cls):
18191819
socket.gethostname(),
18201820
os.getpid(),
18211821
thread.get_ident(),
1822-
int(1e6 * time.time()),
1822+
int(1e6 * mitogen.core.now()),
18231823
)
18241824

18251825
def __repr__(self):
@@ -2569,7 +2569,7 @@ def _on_broker_shutdown(self):
25692569
def _install_timer(self, delay):
25702570
new = self._timer is None
25712571
self._timer = self.broker.timers.schedule(
2572-
when=time.time() + delay,
2572+
when=mitogen.core.now() + delay,
25732573
func=self.reap,
25742574
)
25752575
if new:

mitogen/service.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,7 @@ def get(cls, context, path, out_fp):
11091109
:meth:`fetch`.
11101110
"""
11111111
LOG.debug('get_file(): fetching %r from %r', path, context)
1112-
t0 = time.time()
1112+
t0 = mitogen.core.now()
11131113
recv = mitogen.core.Receiver(router=context.router)
11141114
metadata = context.call_service(
11151115
service_name=cls.name(),
@@ -1143,5 +1143,6 @@ def get(cls, context, path, out_fp):
11431143
path, metadata['size'], received_bytes)
11441144

11451145
LOG.debug('target.get_file(): fetched %d bytes of %r from %r in %dms',
1146-
metadata['size'], path, context, 1000 * (time.time() - t0))
1146+
metadata['size'], path, context,
1147+
1000 * (mitogen.core.now() - t0))
11471148
return ok, metadata

tests/bench/fork.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
"""
44

55
import mitogen
6-
import time
6+
import mitogen.core
77

88
@mitogen.main()
99
def main(router):
10-
t0 = time.time()
10+
t0 = mitogen.core.now()
1111
for x in xrange(200):
12-
t = time.time()
12+
t = mitogen.core.now()
1313
ctx = router.fork()
1414
ctx.shutdown(wait=True)
15-
print '++', 1000 * ((time.time() - t0) / (1.0+x))
15+
print '++', 1000 * ((mitogen.core.now() - t0) / (1.0+x))

tests/bench/large_messages.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import subprocess
55
import time
66
import socket
7+
78
import mitogen
9+
import mitogen.core
810

911

1012
@mitogen.main()
@@ -15,12 +17,12 @@ def main(router):
1517
s = ' ' * n
1618
print('bytes in %.2fMiB string...' % (n/1048576.0),)
1719

18-
t0 = time.time()
20+
t0 = mitogen.core.now()
1921
for x in range(10):
20-
tt0 = time.time()
22+
tt0 = mitogen.core.now()
2123
assert n == c.call(len, s)
22-
print('took %dms' % (1000 * (time.time() - tt0),))
23-
t1 = time.time()
24+
print('took %dms' % (1000 * (mitogen.core.now() - tt0),))
25+
t1 = mitogen.core.now()
2426
print('total %dms / %dms avg / %.2fMiB/sec' % (
2527
1000 * (t1 - t0),
2628
(1000 * (t1 - t0)) / (x + 1),

tests/bench/latch_roundtrip.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import time
77

88
import mitogen
9+
import mitogen.core
910
import mitogen.utils
1011
import ansible_mitogen.affinity
1112

@@ -33,8 +34,8 @@ def flip_flop(ready, inp, out):
3334
ready.get()
3435
ready.get()
3536

36-
t0 = time.time()
37+
t0 = mitogen.core.now()
3738
l1.put(None)
3839
t1.join()
3940
t2.join()
40-
print('++', int(1e6 * ((time.time() - t0) / (1.0+X))), 'usec')
41+
print('++', int(1e6 * ((mitogen.core.now() - t0) / (1.0+X))), 'usec')

tests/bench/local.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import time
66

77
import mitogen
8+
import mitogen.core
89
import mitogen.utils
910
import ansible_mitogen.affinity
1011

@@ -15,10 +16,10 @@
1516

1617
@mitogen.main()
1718
def main(router):
18-
t0=time.time()
19+
t0 = mitogen.core.now()
1920
for x in range(100):
20-
t = time.time()
21+
t = mitogen.core.now()
2122
f = router.local()# debug=True)
22-
tt = time.time()
23+
tt = mitogen.core.now()
2324
print(x, 1000 * (tt - t))
24-
print('%.03f ms' % (1000 * (time.time() - t0) / (1.0 + x)))
25+
print('%.03f ms' % (1000 * (mitogen.core.now() - t0) / (1.0 + x)))

tests/bench/megatime.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
import os
55
import time
66

7+
import mitogen.core
8+
79

810
times = []
911
for x in range(5):
10-
t0 = time.time()
12+
t0 = mitogen.core.now()
1113
os.spawnvp(os.P_WAIT, sys.argv[1], sys.argv[1:])
12-
t = time.time() - t0
14+
t = mitogen.core.now() - t0
1315
times.append(t)
1416
print('+++', t)
1517

0 commit comments

Comments
 (0)