-
-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add app_factory for uvicorn multi-worker support #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,17 +11,21 @@ | |
| host = os.getenv("HOST", "127.0.0.1") | ||
| port = int(os.getenv("PORT", "8000")) | ||
| debug = os.getenv("DEBUG", "false").lower() == "true" | ||
| workers = int(os.getenv("API_WORKERS", "4")) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Default should be
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, obviously you're right. In order for this PR to be complete, it should address the concurrent writes issue (moving away from SQLite? serializing?). So we can either do this one as ground work and address these issues later, or just postpone this PR until there's a fuller solution. This is your baby - your call. Feel free to edit this PR with workers=1 or just discard it to come back to it later. I'm fine either way :-) |
||
|
|
||
| print(f"Starting Shelly Manager API on {host}:{port}") | ||
| print(f"Debug mode: {debug}") | ||
| print(f"Workers: {1 if debug else workers} ({'reload mode' if debug else 'multi-worker'})") | ||
| print(f"OpenAPI docs: http://{host}:{port}/docs") | ||
| print(f"OpenAPI JSON: http://{host}:{port}/openapi.json") | ||
| print(f"Health check: http://{host}:{port}/api/health") | ||
|
|
||
| uvicorn.run( | ||
| "api.main:app", | ||
| "api.main:app_factory", | ||
| host=host, | ||
| port=port, | ||
| factory=True, | ||
| workers=1 if debug else workers, | ||
| reload=debug, | ||
| log_level="info" if not debug else "debug", | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -93,3 +93,6 @@ async def lifespan(app: Litestar) -> AsyncGenerator[None, None]: | |
|
|
||
|
|
||
| app = create_app() | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we're going to use app_factory, can we remove this one? |
||
|
|
||
| def app_factory() -> Litestar: | ||
| return create_app() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here