Skip to content

Commit 79335e1

Browse files
Use time.monotonic() instead of datetime.datetime.now() (#23)
* Use `time.monotonic` * Update changelog --------- Co-authored-by: luccabb <[email protected]>
1 parent 8940562 commit 79335e1

File tree

4 files changed

+26
-35
lines changed

4 files changed

+26
-35
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Added `logging.LoggerAdapter` to `_MaybeLogger` union https://github.com/python-backoff/backoff/pull/34 (from @jcbertin)
1111
- Add `exception` to the typing-only `Details` dictionary for cases when `on_exception` is used https://github.com/python-backoff/backoff/pull/35 (from @edgarrmondragon)
1212
- Add GitHub Actions for CI, documentation, and publishing https://github.com/python-backoff/backoff/pull/39 (from @tysoncung)
13+
- Use `time.monotonic` instead of `datetime.datetime.now` https://github.com/python-backoff/backoff/pull/23 (from @luccabb)
1314

1415
### Packaging
1516

backoff/_async.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
# coding:utf-8
2-
import datetime
3-
import functools
42
import asyncio
3+
import functools
54
import inspect
6-
from datetime import timedelta
5+
import time
76

8-
from backoff._common import (_init_wait_gen, _maybe_call, _next_wait)
7+
from backoff._common import _init_wait_gen, _maybe_call, _next_wait
98

109

1110
def _ensure_coroutine(coro_or_func):
@@ -61,11 +60,11 @@ async def retry(*args, **kwargs):
6160
max_time_value = _maybe_call(max_time)
6261

6362
tries = 0
64-
start = datetime.datetime.now()
63+
start = time.monotonic()
6564
wait = _init_wait_gen(wait_gen, wait_gen_kwargs)
6665
while True:
6766
tries += 1
68-
elapsed = timedelta.total_seconds(datetime.datetime.now() - start)
67+
elapsed = time.monotonic() - start
6968
details = {
7069
"target": target,
7170
"args": args,
@@ -135,11 +134,11 @@ async def retry(*args, **kwargs):
135134
max_time_value = _maybe_call(max_time)
136135

137136
tries = 0
138-
start = datetime.datetime.now()
137+
start = time.monotonic()
139138
wait = _init_wait_gen(wait_gen, wait_gen_kwargs)
140139
while True:
141140
tries += 1
142-
elapsed = timedelta.total_seconds(datetime.datetime.now() - start)
141+
elapsed = time.monotonic() - start
143142
details = {
144143
"target": target,
145144
"args": args,

backoff/_sync.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# coding:utf-8
2-
import datetime
32
import functools
43
import time
5-
from datetime import timedelta
64

75
from backoff._common import (_init_wait_gen, _maybe_call, _next_wait)
86

@@ -32,11 +30,11 @@ def retry(*args, **kwargs):
3230
max_time_value = _maybe_call(max_time)
3331

3432
tries = 0
35-
start = datetime.datetime.now()
33+
start = time.monotonic()
3634
wait = _init_wait_gen(wait_gen, wait_gen_kwargs)
3735
while True:
3836
tries += 1
39-
elapsed = timedelta.total_seconds(datetime.datetime.now() - start)
37+
elapsed = time.monotonic() - start
4038
details = {
4139
"target": target,
4240
"args": args,
@@ -88,11 +86,11 @@ def retry(*args, **kwargs):
8886
max_time_value = _maybe_call(max_time)
8987

9088
tries = 0
91-
start = datetime.datetime.now()
89+
start = time.monotonic()
9290
wait = _init_wait_gen(wait_gen, wait_gen_kwargs)
9391
while True:
9492
tries += 1
95-
elapsed = timedelta.total_seconds(datetime.datetime.now() - start)
93+
elapsed = time.monotonic() - start
9694
details = {
9795
"target": target,
9896
"args": args,

tests/test_backoff.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# coding:utf-8
2-
import datetime
32
import itertools
43
import logging
54
import random
@@ -47,19 +46,17 @@ def return_true(log, n):
4746

4847
def test_on_predicate_max_time(monkeypatch):
4948
nows = [
50-
datetime.datetime(2018, 1, 1, 12, 0, 10, 5),
51-
datetime.datetime(2018, 1, 1, 12, 0, 9, 0),
52-
datetime.datetime(2018, 1, 1, 12, 0, 1, 0),
53-
datetime.datetime(2018, 1, 1, 12, 0, 0, 0),
49+
10.000005,
50+
9,
51+
1,
52+
0
5453
]
5554

56-
class Datetime:
57-
@staticmethod
58-
def now():
59-
return nows.pop()
55+
def monotonic():
56+
return nows.pop()
6057

6158
monkeypatch.setattr('time.sleep', lambda x: None)
62-
monkeypatch.setattr('datetime.datetime', Datetime)
59+
monkeypatch.setattr('time.monotonic', monotonic)
6360

6461
def giveup(details):
6562
assert details['tries'] == 3
@@ -80,19 +77,17 @@ def return_true(log, n):
8077

8178
def test_on_predicate_max_time_callable(monkeypatch):
8279
nows = [
83-
datetime.datetime(2018, 1, 1, 12, 0, 10, 5),
84-
datetime.datetime(2018, 1, 1, 12, 0, 9, 0),
85-
datetime.datetime(2018, 1, 1, 12, 0, 1, 0),
86-
datetime.datetime(2018, 1, 1, 12, 0, 0, 0),
80+
10.000005,
81+
9,
82+
1,
83+
0
8784
]
8885

89-
class Datetime:
90-
@staticmethod
91-
def now():
92-
return nows.pop()
86+
def monotonic():
87+
return nows.pop()
9388

9489
monkeypatch.setattr('time.sleep', lambda x: None)
95-
monkeypatch.setattr('datetime.datetime', Datetime)
90+
monkeypatch.setattr('time.monotonic', monotonic)
9691

9792
def giveup(details):
9893
assert details['tries'] == 3
@@ -502,7 +497,6 @@ def emptiness(*args, **kwargs):
502497
assert len(logger.giveups) == 1
503498

504499
details = dict(logger.giveups[0])
505-
print(details)
506500
elapsed = details.pop('elapsed')
507501
assert isinstance(elapsed, float)
508502
assert details == {'args': (1, 2, 3),
@@ -591,7 +585,6 @@ def success(*args, **kwargs):
591585

592586
for i in range(2):
593587
details = backoffs[i]
594-
print(details)
595588
elapsed = details.pop('elapsed')
596589
assert isinstance(elapsed, float)
597590
assert details == {'args': (1, 2, 3),

0 commit comments

Comments
 (0)