-
Notifications
You must be signed in to change notification settings - Fork 173
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
fix(tasks): Fix @repeat_every blocking app startup #308
Conversation
@dmontagu @yuval9313 please can this be looked at asap |
I'm not sure why those tests don't pass, I did run |
My bad, I don't know what happened with my tests then, but they didn't pass locally neither in the end. They do now ; it was just a missing EDIT: I'm sorry, we're back at the beginning. It blocks FastAPI's context manager. |
fastapi_utils/tasks.py
Outdated
@@ -74,7 +74,7 @@ async def loop() -> None: | |||
repetitions += 1 | |||
await asyncio.sleep(seconds) | |||
|
|||
await loop() | |||
await asyncio.ensure_future(loop()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Keiishu : I think you need to remove the await
. This is what was used in v0.2.1
. I test your code but remove the await
in my local FastAPI project, and it worked. It no longer block the app to start.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ohandyya : That's what I did in the first commit on this PR, and it works for me, I've used it like this with no issues whatsoever in a relatively big FastAPI project. But then, tests were not passing anymore and I thought it was my fault. So I must say I'm a little bit confused.
Maybe the tests are faulty?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not familiar with sarlette but I'm pretty confident that asyncio.ensure_future doesn't return coroutine, therefore await isn't needed. I'm using the below code since the 0.6.0 repeat_every break, and this works fine for me.
def repeat(seconds: float, delay: float = None):
def decorator(func):
is_coroutine = asyncio.iscoroutinefunction(func)
@wraps(func)
async def wrapper(*args, **kwargs):
async def repeat_function():
if delay:
await asyncio.sleep(delay)
while True:
if is_coroutine:
await func(*args, **kwargs)
else:
run_in_threadpool(func, *args, **kwargs)
await asyncio.sleep(seconds)
asyncio.ensure_future(repeat_function())
return wrapper
return decorator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mgkid3310 You're right, await
shouldn't be necessary. But without it, the tests introduced with 0.6.0 fail. And while your code works perfectly fine for me as well, as do the first commit of this PR, the tests all fail.
So yeah, maybe there is an issue with the tests themselves...
I'll revert my second commit (the await), keep this PR as a draft and wait for further insight on what's happening with the tests.
For now, I'll revert my second commit (the |
This reverts commit 3d9ba0d. See the conversation on fastapiutils#308 for further details.
I've reviewed the So my guess is: the |
I'm working on adding a |
#310 I've merged your commits and added my further fixes for tests. I've solved problems for most tests (4 out of 6 failing tests) but failed to solve the remaining 2 before going to bed. Can you take a look at the code? |
Hey, this is still marked with a draft, would you like this PR to be reviewed or the other one? EDIT: |
No problem @yuval9313 ; #310 is a fork of this PR with updated tests, so we should focus there. I'll close this one. |
Nice, thanks! I really can't focus on this right now, until Thursday at least, so I'll take a look at it then if nobody fixed it by then. |
* fix(tasks): Fix @repeat_every blocking app startup * fix(tasks): Add missing await on ensure_future * Revert "fix(tasks): Add missing await on ensure_future" This reverts commit 3d9ba0d. See the conversation on #308 for further details. * added on_complete parameter for pytest purpose * added loop completed state and loop_completed function * formatting improvements (for mypy) * use Union for typing support in older Python versions * on_exception feature added and deprecation warnings for logger and raise_exception added * Fix TestRepeatEveryWithAsynchronousFunction so that it runs with async functions * add pytest-timeout to package and fix typo * fix function type problems by removing conditional types --------- Co-authored-by: Martin <[email protected]> Co-authored-by: Yuval Levi <[email protected]>
Description of the bug
A function using
@repeat_every
blocks app startup on version 0.6.0 when it is called in the context manager.This should fix #305.
Description of the changes
Replacing (https://github.com/dmontagu/fastapi-utils/blob/master/fastapi_utils/tasks.py#L77) with
asyncio.ensure_future(loop())
works.Every test passes for me.