Skip to content

Commit 42ba14f

Browse files
authored
chore: update readme: main.py double exec (#50)
Signed-off-by: Anupam Kumar <[email protected]>
1 parent ce8e260 commit 42ba14f

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,12 @@ To make it easier to create new Nextcloud applications using Python,
44
you can immediately either clone this repository or simply copy it and use it as the basis for your future application.
55

66
By default, linters, pre-commit and other useful little things are configured here.
7+
8+
### Note
9+
10+
When the main.py file is executed, it instantiates the `APP` class and everything outside of the `__main__` block is executed first.
11+
Now when the `__main__` block is executed, the `run_app` function is called with the `"main:APP"` as one argument, which in turn executes the main.py file (`__main__` is not executed here) and starts the application in uvicorn: `run_app("main:APP", log_level="trace")` resulting in double execution of the code outside the `__main__` block.
12+
The argument `"main:APP"` is the name of the module and the name of the application instance. `run_app` or `uvicorn.run` can also work with just `APP` but then the ability to run multiple workers is lost, which might not be desired in some cases like production environments where the app can make use of multiple CPU cores.
13+
Real world example for both cases can be found in [Visionatrix](https://github.com/Visionatrix/Visionatrix/blob/9c9802468b79fb054599b3af68fd05bcc9105375/visionatrix/backend.py#L262-L278).
14+
15+
Any code to be executed in the main module should be placed in the `lifespan` function before the `yield` statement. It is called when the application is started by a uvicorn worker and is executed only once. Global variables can be set and exported here with the `global` keyword.

0 commit comments

Comments
 (0)