[BUG] `repeat_every` does not work as expected when not also annotated with the fastapi startup event
Closed this issue · 7 comments
Describe the bug
If you use the repeat_every
decorator without app.on_event("startup")
, it requires calling in an async context to actually add to loop to asyncio. You can also get it to work by awaiting the resulting decorated function after the fact.
from fastapi_restful.tasks import repeat_every
@repeat_every(seconds=60)
def do_stuff():
""" this is never called """
It must be called from an async context
from fastapi import FastAPI
from fastapi_restful.tasks import repeat_every
app = FastAPI()
@app.on_event("startup") # runs the decoration once, adding the loop to asyncio
@repeat_every(seconds=60)
def do_stuff():
""" this is never called """
Expected behavior
The decorated function is repeatedly called without
Environment:
- OS: Any (I repro'd on Linux)
- FastAPI Utils, FastAPI, and Pydantic versions [e.g.
0.3.0
], get them with:
fastapi_restful version: 0.4.3
fastapi version: 0.74.0
pydantic version: 1.9.0
pydantic compiled: True
python version: 3.9.0 (default, Jun 4 2021, 15:43:34) [GCC 8.3.1 20190311 (Red Hat 8.3.1-3)]
platform: Linux-3.10.0-1160.45.1.el7.x86_64-x86_64-with-glibc2.17
optional deps. installed: ['dotenv', 'typing-extensions']
Just added an issue for this same bug on the original repository before seeing this. Any movement here or a quick example of a workaround would be super helpful!
I see @DanielEidlin was working on this issue but forgot to add this PR
Excited to see this issue finally fixed. Hate having to add the @app.on_event("startup") to get a repeated task to run. @DCMattyG
Is this issue really fixed?
I still cannot get this to run, without adding a @app.on_event("startup")
:
@repeat_every(seconds=5)
async def say_hi():
print("hi")
try use tornado's PeriodicCallback?
from tornado.ioloop import PeriodicCallback
I had some merge conflicts in the PR of @DanielEidlin who seem to abandon his PR
I'll fix it soon enough
+1 to get his PR fixed!