I have a Python file named main.py
. I am running it on Python 3.9.13 on Windows.
import uvicorn
from fastapi import FastAPI
app = FastAPI()
@app.post('/c')
async def c(b: str):
print(a)
if __name__ == '__main__':
a = load_embeddings('embeddings')
uvicorn.run('main:app', host='127.0.0.1', port=80)
Running this, then invoking POST /c will cause a 500 error with NameError 'a' is not defined.
However it is obvious that a
will be defined first before the server is ran. If I move a
outside of the if __name__ == '__main__':
then it works, but it causes load_embeddings
to be ran multiple times for unknown reasons (3 exact). Since load_embeddings
for me takes long time, I do not want the duplicate execution.
I wish to look for either of these as a solution to my issue: stop whatever outside if __name__ == '__main__':
from executing multiple times, OR make a
defined globally when it is being defined under if __name__ == '__main__':
.
Note: variable names are intentionally renamed for ease of reading. Please do not advise me anything on coding style/naming conventions. I know the community is helpful but that's not the point here, thanks.