diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e76074..d0b828d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - Added `logging.LoggerAdapter` to `_MaybeLogger` union https://github.com/python-backoff/backoff/pull/34 (from @jcbertin) - 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) - Add GitHub Actions for CI, documentation, and publishing https://github.com/python-backoff/backoff/pull/39 (from @tysoncung) +- Use `time.monotonic` instead of `datetime.datetime.now` https://github.com/python-backoff/backoff/pull/23 (from @luccabb) ### Packaging diff --git a/backoff/_async.py b/backoff/_async.py index b9e8d3e..7c950b2 100644 --- a/backoff/_async.py +++ b/backoff/_async.py @@ -1,11 +1,10 @@ # coding:utf-8 -import datetime -import functools import asyncio +import functools import inspect -from datetime import timedelta +import time -from backoff._common import (_init_wait_gen, _maybe_call, _next_wait) +from backoff._common import _init_wait_gen, _maybe_call, _next_wait def _ensure_coroutine(coro_or_func): @@ -61,11 +60,11 @@ async def retry(*args, **kwargs): max_time_value = _maybe_call(max_time) tries = 0 - start = datetime.datetime.now() + start = time.monotonic() wait = _init_wait_gen(wait_gen, wait_gen_kwargs) while True: tries += 1 - elapsed = timedelta.total_seconds(datetime.datetime.now() - start) + elapsed = time.monotonic() - start details = { "target": target, "args": args, @@ -135,11 +134,11 @@ async def retry(*args, **kwargs): max_time_value = _maybe_call(max_time) tries = 0 - start = datetime.datetime.now() + start = time.monotonic() wait = _init_wait_gen(wait_gen, wait_gen_kwargs) while True: tries += 1 - elapsed = timedelta.total_seconds(datetime.datetime.now() - start) + elapsed = time.monotonic() - start details = { "target": target, "args": args, diff --git a/backoff/_sync.py b/backoff/_sync.py index 4371e16..d8d0046 100644 --- a/backoff/_sync.py +++ b/backoff/_sync.py @@ -1,8 +1,6 @@ # coding:utf-8 -import datetime import functools import time -from datetime import timedelta from backoff._common import (_init_wait_gen, _maybe_call, _next_wait) @@ -32,11 +30,11 @@ def retry(*args, **kwargs): max_time_value = _maybe_call(max_time) tries = 0 - start = datetime.datetime.now() + start = time.monotonic() wait = _init_wait_gen(wait_gen, wait_gen_kwargs) while True: tries += 1 - elapsed = timedelta.total_seconds(datetime.datetime.now() - start) + elapsed = time.monotonic() - start details = { "target": target, "args": args, @@ -88,11 +86,11 @@ def retry(*args, **kwargs): max_time_value = _maybe_call(max_time) tries = 0 - start = datetime.datetime.now() + start = time.monotonic() wait = _init_wait_gen(wait_gen, wait_gen_kwargs) while True: tries += 1 - elapsed = timedelta.total_seconds(datetime.datetime.now() - start) + elapsed = time.monotonic() - start details = { "target": target, "args": args, diff --git a/tests/test_backoff.py b/tests/test_backoff.py index 800756a..7d46269 100644 --- a/tests/test_backoff.py +++ b/tests/test_backoff.py @@ -1,5 +1,4 @@ # coding:utf-8 -import datetime import itertools import logging import random @@ -47,19 +46,17 @@ def return_true(log, n): def test_on_predicate_max_time(monkeypatch): nows = [ - datetime.datetime(2018, 1, 1, 12, 0, 10, 5), - datetime.datetime(2018, 1, 1, 12, 0, 9, 0), - datetime.datetime(2018, 1, 1, 12, 0, 1, 0), - datetime.datetime(2018, 1, 1, 12, 0, 0, 0), + 10.000005, + 9, + 1, + 0 ] - class Datetime: - @staticmethod - def now(): - return nows.pop() + def monotonic(): + return nows.pop() monkeypatch.setattr('time.sleep', lambda x: None) - monkeypatch.setattr('datetime.datetime', Datetime) + monkeypatch.setattr('time.monotonic', monotonic) def giveup(details): assert details['tries'] == 3 @@ -80,19 +77,17 @@ def return_true(log, n): def test_on_predicate_max_time_callable(monkeypatch): nows = [ - datetime.datetime(2018, 1, 1, 12, 0, 10, 5), - datetime.datetime(2018, 1, 1, 12, 0, 9, 0), - datetime.datetime(2018, 1, 1, 12, 0, 1, 0), - datetime.datetime(2018, 1, 1, 12, 0, 0, 0), + 10.000005, + 9, + 1, + 0 ] - class Datetime: - @staticmethod - def now(): - return nows.pop() + def monotonic(): + return nows.pop() monkeypatch.setattr('time.sleep', lambda x: None) - monkeypatch.setattr('datetime.datetime', Datetime) + monkeypatch.setattr('time.monotonic', monotonic) def giveup(details): assert details['tries'] == 3 @@ -502,7 +497,6 @@ def emptiness(*args, **kwargs): assert len(logger.giveups) == 1 details = dict(logger.giveups[0]) - print(details) elapsed = details.pop('elapsed') assert isinstance(elapsed, float) assert details == {'args': (1, 2, 3), @@ -591,7 +585,6 @@ def success(*args, **kwargs): for i in range(2): details = backoffs[i] - print(details) elapsed = details.pop('elapsed') assert isinstance(elapsed, float) assert details == {'args': (1, 2, 3),