Reputation: 31
My app with lifespan
defined like this:
@contextlib.asynccontextmanager
async def lifespan(app:fa.FastAPI):
print("LIFE SPAN: Start up")
yield
print("LIFE SPAN: Shutdown")
app_api = fa.FastAPI(lifespan=lifespan)
app_api.include_router(test.router)
inside retrofun/___main___.py .
from retrofun.api import app_api
import argparse
import uvicorn
def main():
parser = argparse.ArgumentParser(
description="Run the RetroFun application.")
parser.add_argument(
"-p", "--host-port", type=str, default="localhost:8000",
help="Specify the host and port with format host:port (default: localhost:8000)")
args = parser.parse_args()
host,port=args.host_port.split(':')
port = int(port)
print(f"Starting retrofun at {args.host_port} ...")
uvicorn.run(
"retrofun.__main__:app_api",
host=host,
port=port,
reload=True
)
if __name__=="__main__":
main()
Case 1: When I run python -m retrofun
. Everything including lifespan works well.
Case 2: With docker file:
CMD ["uv","run","-m","retrofun","-p","0.0.0.0:5555"]
Everything works well like intent EXCEPT lifespan does not work (print to console) anymore.
Case 3: If i change the CMD in Dockerfile with another like:
CMD ["uv","run","fastapi","run","./src/retrofun/__main__.py"]
Lifespan works again.
Question: Why lifespan does not work in case 2 and how to resolve that? Thank you.
Upvotes: 3
Views: 42