11# coding:utf-8
2- import datetime
2+ import time
33import functools
44import asyncio
5- from datetime import timedelta
65
76from backoff ._common import (_init_wait_gen , _maybe_call , _next_wait )
87
@@ -41,6 +40,8 @@ def retry_predicate(target, wait_gen, predicate,
4140 * ,
4241 max_tries , max_time , jitter ,
4342 on_success , on_backoff , on_giveup ,
43+ monotonic_time = None ,
44+ sleep = None ,
4445 wait_gen_kwargs ):
4546 on_success = _ensure_coroutines (on_success )
4647 on_backoff = _ensure_coroutines (on_backoff )
@@ -60,11 +61,11 @@ async def retry(*args, **kwargs):
6061 max_time_value = _maybe_call (max_time )
6162
6263 tries = 0
63- start = datetime . datetime . now ()
64+ start = ( monotonic_time or time . monotonic ) ()
6465 wait = _init_wait_gen (wait_gen , wait_gen_kwargs )
6566 while True :
6667 tries += 1
67- elapsed = timedelta . total_seconds ( datetime . datetime . now () - start )
68+ elapsed = ( monotonic_time or time . monotonic ) () - start
6869 details = {
6970 "target" : target ,
7071 "args" : args ,
@@ -102,7 +103,7 @@ async def retry(*args, **kwargs):
102103 # See for details:
103104 # <https://groups.google.com/forum/#!topic/python-tulip/yF9C-rFpiKk>
104105 # <https://bugs.python.org/issue28613>
105- await asyncio .sleep (seconds )
106+ await ( sleep or asyncio .sleep ) (seconds )
106107 continue
107108 else :
108109 await _call_handlers (on_success , ** details , value = ret )
@@ -117,6 +118,7 @@ def retry_exception(target, wait_gen, exception,
117118 * ,
118119 max_tries , max_time , jitter , giveup ,
119120 on_success , on_backoff , on_giveup , raise_on_giveup ,
121+ sleep = None , monotonic_time = None ,
120122 wait_gen_kwargs ):
121123 on_success = _ensure_coroutines (on_success )
122124 on_backoff = _ensure_coroutines (on_backoff )
@@ -134,11 +136,11 @@ async def retry(*args, **kwargs):
134136 max_time_value = _maybe_call (max_time )
135137
136138 tries = 0
137- start = datetime . datetime . now ()
139+ start = ( monotonic_time or time . monotonic ) ()
138140 wait = _init_wait_gen (wait_gen , wait_gen_kwargs )
139141 while True :
140142 tries += 1
141- elapsed = timedelta . total_seconds ( datetime . datetime . now () - start )
143+ elapsed = ( monotonic_time or time . monotonic ) () - start
142144 details = {
143145 "target" : target ,
144146 "args" : args ,
@@ -180,7 +182,7 @@ async def retry(*args, **kwargs):
180182 # See for details:
181183 # <https://groups.google.com/forum/#!topic/python-tulip/yF9C-rFpiKk>
182184 # <https://bugs.python.org/issue28613>
183- await asyncio .sleep (seconds )
185+ await ( sleep or asyncio .sleep ) (seconds )
184186 else :
185187 await _call_handlers (on_success , ** details )
186188
0 commit comments