Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asynq/tools: update aretry to compatible with asynq_to_asyncio #151

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion asynq/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import functools
import inspect
import itertools
import asyncio
import threading
import time
import weakref
Expand Down Expand Up @@ -282,6 +283,12 @@ def dirty():

return decorator

async def _asyncio_sleep(secs: float) -> None:
await asyncio.sleep(secs)

@asynq(asyncio_fn=_asyncio_sleep)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@asynq(asyncio_fn=_asyncio_sleep)
@asynq(asyncio_fn=asyncio.sleep)

I don't think you need the wrapper function?

def _asynq_sleep(secs: float) -> None:
time.sleep(secs)

def aretry(exception_cls, max_tries=10, sleep=0.05):
"""Decorator for retrying an async function if it throws an exception.
Expand All @@ -290,6 +297,8 @@ def aretry(exception_cls, max_tries=10, sleep=0.05):
max_tries - maximum number of times this function can be executed
sleep - number of seconds to sleep between function retries

Compatible with asyncio mode.

"""
assert max_tries > 0, "max_tries (%d) should be a positive integer" % max_tries

Expand All @@ -304,7 +313,7 @@ def wrapper(*args, **kwargs):
except exception_cls:
if i + 1 == max_tries:
raise
time.sleep(sleep)
yield _asynq_sleep.asynq(sleep)

# so that qcore.inspection.get_original_fn can retrieve the original function
wrapper.original_fn = fn
Expand Down
Loading