'Running uvicorn with -OO flag

When I run a uvicorn server from the commandline by $ uvicorn example:app or $ gunicorn -w 4 -k uvicorn.workers.UvicornWorker, how can I pass the optimization flag -OO to the Python interpreter? Any help would be appreciated!



Solution 1:[1]

You probably need to run uvicorn programmatically and then run the code with -O.

# main.py
import uvicorn

class App:
   ...

app = App()

if __name__ == "__main__":
    uvicorn.run("main:app", host="127.0.0.1", port=5000)

Then you run it with python -O main.py

A way to verify if the code is actually running with the -O flag is to use asserts. Assert statements are removed from optimized code so you can add this block of code somewhere in the main.py

try:
    assert False
except AssertionError:
    print("Warning: code is not optimized")
else:
    print("Running optimized code")

Solution 2:[2]

For example, we have the following app:

from fastapi import FastAPI

app = FastAPI()


@app.on_event('startup')
async def startup_event():
    print('__debug__', __debug__)

unicorn can be run like this:

$ python -O -m uvicorn example:app
INFO:     Started server process [49759]
INFO:     Waiting for application startup.
__debug__ False
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

gunicorn run with:

$ python -O -m gunicorn --workers 4 --worker-class uvicorn.workers.UvicornWorker example:app
[2022-02-13 15:17:39 +0300] [49801] [INFO] Starting gunicorn 20.1.0
[2022-02-13 15:17:39 +0300] [49801] [INFO] Listening at: http://127.0.0.1:8000 (49801)
[2022-02-13 15:17:39 +0300] [49801] [INFO] Using worker: uvicorn.workers.UvicornWorker
[2022-02-13 15:17:39 +0300] [49802] [INFO] Booting worker with pid: 49802
[2022-02-13 15:17:39 +0300] [49802] [INFO] Started server process [49802]
[2022-02-13 15:17:39 +0300] [49802] [INFO] Waiting for application startup.
__debug__ False
[2022-02-13 15:17:39 +0300] [49802] [INFO] Application startup complete.
[2022-02-13 15:17:39 +0300] [49804] [INFO] Booting worker with pid: 49804
[2022-02-13 15:17:39 +0300] [49805] [INFO] Booting worker with pid: 49805
[2022-02-13 15:17:39 +0300] [49804] [INFO] Started server process [49804]
[2022-02-13 15:17:39 +0300] [49804] [INFO] Waiting for application startup.
__debug__ False
[2022-02-13 15:17:39 +0300] [49804] [INFO] Application startup complete.
[2022-02-13 15:17:39 +0300] [49805] [INFO] Started server process [49805]
[2022-02-13 15:17:39 +0300] [49805] [INFO] Waiting for application startup.
__debug__ False
[2022-02-13 15:17:39 +0300] [49805] [INFO] Application startup complete.
[2022-02-13 15:17:39 +0300] [49806] [INFO] Booting worker with pid: 49806
[2022-02-13 15:17:39 +0300] [49806] [INFO] Started server process [49806]
[2022-02-13 15:17:39 +0300] [49806] [INFO] Waiting for application startup.
__debug__ False
[2022-02-13 15:17:39 +0300] [49806] [INFO] Application startup complete.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BangTheBank
Solution 2 poofeg