Home > database >  Why does running a python file inside a pod not have the same behavior as running it directly?
Why does running a python file inside a pod not have the same behavior as running it directly?

Time:05-02

I have a FastAPI app with the following code

    @app.on_event("startup")
    async def startup_event():
    """Initialize application services"""
        print("Starting the service")

when I run FastAPI directly from the terminal, I get the following output

INFO:     Started server process [259936]
INFO:     Waiting for application startup.
Starting the service
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:3000 (Press CTRL C to quit)

You can see that the print statement got executed.

However, when the same app is automatically run inside a Kubernetes cluster, I get the following output

 INFO:     Waiting for application startup.
 INFO:     Application startup complete.
 INFO:     Uvicorn running on http://0.0.0.0:3000 (Press CTRL C to quit)

The print statement did not get executed, in fact, any additional code inside the function never gets executed.

However, if I exit the process like this:

@app.on_event("startup")
async def startup_event():
    """Initialize application services"""
    print("Starting the service")
    exit(99)

The process exists then I can see the print statement.

SystemExit: 99
ERROR:    Application startup failed. Exiting.
Starting the service

What is the problem here?

Edit: Actually no code whatsoever gets executed, I have put print statements literally everywhere and nothing gets printed, but somehow the webserver runs...

CodePudding user response:

So, actually, there is no problem with my code, FastAPI, asyncio, or Kubernetes.

Everything was actually working correctly, it's just that the output was buffered.

After adding flush=True to the print statement, everything showed.

I am answering this in case some poor soul stumbles upon this thread in the future.

I spent days debugging this!!!

  • Related