Skip to content

feat: add app_factory for uvicorn multi-worker support#44

Open
doron1 wants to merge 1 commit into
jfmlima:mainfrom
doron1:patch2-workers
Open

feat: add app_factory for uvicorn multi-worker support#44
doron1 wants to merge 1 commit into
jfmlima:mainfrom
doron1:patch2-workers

Conversation

@doron1
Copy link
Copy Markdown

@doron1 doron1 commented May 19, 2026

If you choose to merge this one, a follow-on patch to unraid-templates should also be merged

Copy link
Copy Markdown
Owner

@jfmlima jfmlima left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again @doron1.

Some notes:
The app_factory pattern is solid and I'd like to take it, however, defaulting to 4 workers with SQLite storage is unsafe and will cause database is locked errors under concurrent writes and breaks in-memory auth caching (each worker gets its own AuthStateCache).

Lets ship app_factory with --factory and workers=1 and tackle multi-worker in a follow-up.

Also: #43 should be merged first 👍

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"))
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default should be "1" not "4" until SQLite concurrency is addressed

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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 :-)

@@ -93,3 +93,6 @@ async def lifespan(app: Litestar) -> AsyncGenerator[None, None]:


app = create_app()
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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?

Comment thread packages/api/Dockerfile
EXPOSE 8000

CMD ["sh", "-c", "uvicorn api.main:app --host ${HOST} --port ${PORT} --log-level info"]
CMD ["sh", "-c", "uvicorn api.main:app_factory --factory --host ${HOST} --port ${PORT} --log-level info --workers ${API_WORKERS:-4}"]
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

@doron1 doron1 requested a review from jfmlima May 22, 2026 21:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants