1
1
# coding:utf-8
2
- import datetime
2
+ import time
3
3
import functools
4
4
import asyncio
5
- from datetime import timedelta
6
5
7
6
from backoff ._common import (_init_wait_gen , _maybe_call , _next_wait )
8
7
@@ -41,6 +40,8 @@ def retry_predicate(target, wait_gen, predicate,
41
40
* ,
42
41
max_tries , max_time , jitter ,
43
42
on_success , on_backoff , on_giveup ,
43
+ monotonic_time = None ,
44
+ sleep = None ,
44
45
wait_gen_kwargs ):
45
46
on_success = _ensure_coroutines (on_success )
46
47
on_backoff = _ensure_coroutines (on_backoff )
@@ -60,11 +61,11 @@ async def retry(*args, **kwargs):
60
61
max_time_value = _maybe_call (max_time )
61
62
62
63
tries = 0
63
- start = datetime . datetime . now ()
64
+ start = ( monotonic_time or time . monotonic ) ()
64
65
wait = _init_wait_gen (wait_gen , wait_gen_kwargs )
65
66
while True :
66
67
tries += 1
67
- elapsed = timedelta . total_seconds ( datetime . datetime . now () - start )
68
+ elapsed = ( monotonic_time or time . monotonic ) () - start
68
69
details = {
69
70
"target" : target ,
70
71
"args" : args ,
@@ -102,7 +103,7 @@ async def retry(*args, **kwargs):
102
103
# See for details:
103
104
# <https://groups.google.com/forum/#!topic/python-tulip/yF9C-rFpiKk>
104
105
# <https://bugs.python.org/issue28613>
105
- await asyncio .sleep (seconds )
106
+ await ( sleep or asyncio .sleep ) (seconds )
106
107
continue
107
108
else :
108
109
await _call_handlers (on_success , ** details , value = ret )
@@ -117,6 +118,7 @@ def retry_exception(target, wait_gen, exception,
117
118
* ,
118
119
max_tries , max_time , jitter , giveup ,
119
120
on_success , on_backoff , on_giveup , raise_on_giveup ,
121
+ sleep = None , monotonic_time = None ,
120
122
wait_gen_kwargs ):
121
123
on_success = _ensure_coroutines (on_success )
122
124
on_backoff = _ensure_coroutines (on_backoff )
@@ -134,11 +136,11 @@ async def retry(*args, **kwargs):
134
136
max_time_value = _maybe_call (max_time )
135
137
136
138
tries = 0
137
- start = datetime . datetime . now ()
139
+ start = ( monotonic_time or time . monotonic ) ()
138
140
wait = _init_wait_gen (wait_gen , wait_gen_kwargs )
139
141
while True :
140
142
tries += 1
141
- elapsed = timedelta . total_seconds ( datetime . datetime . now () - start )
143
+ elapsed = ( monotonic_time or time . monotonic ) () - start
142
144
details = {
143
145
"target" : target ,
144
146
"args" : args ,
@@ -180,7 +182,7 @@ async def retry(*args, **kwargs):
180
182
# See for details:
181
183
# <https://groups.google.com/forum/#!topic/python-tulip/yF9C-rFpiKk>
182
184
# <https://bugs.python.org/issue28613>
183
- await asyncio .sleep (seconds )
185
+ await ( sleep or asyncio .sleep ) (seconds )
184
186
else :
185
187
await _call_handlers (on_success , ** details )
186
188
0 commit comments