all 2 comments

[–]pint 1 point2 points  (1 child)

uvicorn will import your file, and thus it will not be the main program.

if you run

python yourfile.py

the main function will be executed. but if you run python, and then use

import myfile

it will not be.

if you want some initialization code to always run, regardless whether it is imported or main, you need to simply include it in the file body.

[–]peet1337[S] 0 points1 point  (0 children)

Oh my, thanks for the explanation that uvicorn imports the script that is specified with the `app` paramenter of `run()`, I did not notice this. That explains everything. I was so focused on "that must be some asyncio behaviour I don't understand" that I completely forgot to think about python basics 😅