Skip to content
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

Unique Worker ID #2529

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Revert "use more modern version for types"
This reverts commit 9b49172.
  • Loading branch information
whilenot-dev committed Dec 5, 2024
commit 339d6445aea1113a5d6ef0ef4fbcf3c3bc1c9918
21 changes: 9 additions & 12 deletions uvicorn/lifespan/on.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import asyncio
import logging
from asyncio import Queue
from typing import Any
from typing import Any, Optional, Union, cast

from uvicorn import Config
from uvicorn._types import (
Expand All @@ -16,16 +16,13 @@
LifespanStartupFailedEvent,
)

LifespanReceiveMessage = (
LifespanStartupEvent
| LifespanShutdownEvent
)
LifespanSendMessage = (
LifespanStartupFailedEvent
| LifespanShutdownFailedEvent
| LifespanStartupCompleteEvent
| LifespanShutdownCompleteEvent
)
LifespanReceiveMessage = Union[LifespanStartupEvent, LifespanShutdownEvent]
LifespanSendMessage = Union[
LifespanStartupFailedEvent,
LifespanShutdownFailedEvent,
LifespanStartupCompleteEvent,
LifespanShutdownCompleteEvent,
]


STATE_TRANSITION_ERROR = "Got invalid state transition on lifespan protocol."
Expand Down Expand Up @@ -83,7 +80,7 @@ async def main(self) -> None:
app = self.config.loaded_app

# inject worker id into app state
uvicorn_worker_id: int | None = self.state.get("uvicorn_worker_id")
uvicorn_worker_id = cast(Optional[int], self.state.get("uvicorn_worker_id"))
if uvicorn_worker_id is not None and hasattr(app.app, "__iter__") and "state" in app.app:
app.app.state.uvicorn_worker_id = uvicorn_worker_id

Expand Down
  NODES
COMMUNITY 2
Note 1
Project 2
USERS 1