backoff icon indicating copy to clipboard operation
backoff copied to clipboard

Allow Custom Sleep Function

Open stevenjackson121 opened this issue 3 years ago • 3 comments

I think it would be great to add a sleep argument which defaults to asyncio.sleep to improve support for event loops besides asyncio, while keeping it working out of the box for asyncio. I like backoff and would like to stick with it, but it ended up being too much of a hassle to get it working with trio.

tenacity has this feature, with the following syntax/explanation.

You can even use alternative event loops such as curio or Trio by passing the correct sleep function:
    
    @retry(sleep=trio.sleep)
    async def my_async_function(loop):
        await asks.get('https://example.org')

stevenjackson121 avatar Apr 23 '21 18:04 stevenjackson121

Is this just as simple as awaiting a different sleep function here?

https://github.com/litl/backoff/blob/master/backoff/_async.py#L94

if I'm understanding it looks like this would be very easy to accomodate

bgreen-litl avatar Apr 23 '21 19:04 bgreen-litl

Yes. That should be it! (Of course also adding keyword argument and passing it through as well)

stevenjackson121 avatar Apr 25 '21 03:04 stevenjackson121