Skip to content

Commit

Permalink
treat uvicorn_worker_id as optional in lifespan state
Browse files Browse the repository at this point in the history
  • Loading branch information
whilenot-dev committed Dec 5, 2024
1 parent fe854b4 commit 976df96
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 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, Union
from typing import cast, Any, Optional, Union

from uvicorn import Config
from uvicorn._types import (
Expand Down Expand Up @@ -80,7 +80,9 @@ async def main(self) -> None:
app = self.config.loaded_app

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

scope: LifespanScope = {
"type": "lifespan",
Expand Down

0 comments on commit 976df96

Please sign in to comment.