1

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.

Billy Cao
  • 335
  • 4
  • 15

1 Answers1

0

You can resolve the issue by moving the a variable definition inside the c function. Then, you can add a check inside the function to load the embeddings only if they have not been loaded yet. You can achieve this by using a global variable, which will keep track of whether the embeddings have been loaded or not.

Here is an example:

import uvicorn
from fastapi import FastAPI

app = FastAPI()

EMBEDDINGS_LOADED = False

def load_embeddings(filename):
# Load embeddings code here
...

@app.post('/c')
async def c(b: str):
global EMBEDDINGS_LOADED
if not EMBEDDINGS_LOADED:
    load_embeddings('embeddings')
    EMBEDDINGS_LOADED = True
print(a)

if __name__ == '__main__':
uvicorn.run('main:app', host='127.0.0.1', port=80)
MRRaja
  • 1,073
  • 12
  • 25
  • thanks, i have already implemented this as a workaround, but it adds unnecessary code that I do not wish to add - because I have tens of variables like `a` - cant do that for every place that needs it – Billy Cao Feb 05 '23 at 13:18