dmontagu / fastapi-utils

Reusable utilities for FastAPI
MIT License
1.84k stars 163 forks source link

[BUG] repeat_every now blocks application startup #307

Closed gabriel-faria-zipline closed 1 month ago

gabriel-faria-zipline commented 2 months ago

Describe the bug On v.0.6.0 the @repeat_every will prevent the application from starting up.

To Reproduce A simple @repeat_every implementation:

import logging
from contextlib import asynccontextmanager

import uvicorn
from fastapi import FastAPI
from fastapi_utils.tasks import repeat_every

logging.basicConfig(level=logging.INFO, force=True)
logger = logging.getLogger(__name__)

@asynccontextmanager
async def lifespan(app: FastAPI):
    await do_something()
    yield

app = FastAPI(lifespan=lifespan)

@repeat_every(seconds=1, logger=logger)
async def do_something() -> None:
    logger.info("REPETITION EXECUTED.")
    return

@app.get("/")
async def health():
    return 'Success'

if __name__ == "__main__":
    uvicorn.run(app, host="localhost", port=8080)

Expected behavior The app starts showing: (which works on v0.2.0

$ python main.py                                        
INFO:     Started server process [4160]
INFO:     Waiting for application startup.
INFO:__main__:REPETITION EXECUTED.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://localhost:8080 (Press CTRL+C to quit)
INFO:__main__:REPETITION EXECUTED.

But in v0.6.0 the server is blocked by repeat_every execution:

$ python main.py                                          
INFO:     Started server process [4223]
INFO:     Waiting for application startup.     <<<< It never gets past this
INFO:__main__:REPETITION EXECUTED.

Environment:

Python 3.12.2

ollz272 commented 2 months ago

This is already being tracked: https://github.com/dmontagu/fastapi-utils/issues/305

yuval9313 commented 1 month ago

Indeed duplicate