Skip to content

Commit

Permalink
add state to lifespan scope
Browse files Browse the repository at this point in the history
  • Loading branch information
kramstrom committed Sep 4, 2024
1 parent aaf376e commit 269fb4e
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions modal/_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@


class LifespanManager:
def __init__(self, asgi_app):
def __init__(self, asgi_app, state):
self.asgi_app = asgi_app
self.queue = None
self.startup_complete = None
self.state = state

async def background_task(self):
self.queue = asyncio.Queue()
Expand All @@ -41,7 +42,7 @@ async def send(message):
else:
raise ValueError(f"Unexpected message type: {message['type']}")

await self.asgi_app({"type": "lifespan"}, receive, send)
await self.asgi_app({"type": "lifespan", "state": self.state}, receive, send)

async def lifespan_startup(self):
if self.queue is None or self.shutdown is None:
Expand All @@ -61,7 +62,12 @@ async def lifespan_shutdown(self):
def asgi_app_wrapper(
asgi_app, function_io_manager
) -> Tuple[Callable[..., AsyncGenerator], Callable[..., Awaitable[None]], Callable[..., Awaitable[None]]]:
state = {} # used for lifespan state

async def fn(scope):
if "state" in scope:
raise ValueError("Unpexected state in ASGI scope")
scope["state"] = state
function_call_id = current_function_call_id()
assert function_call_id, "internal error: function_call_id not set in asgi_app() scope"

Expand Down Expand Up @@ -173,7 +179,7 @@ async def receive():
app_task.result() # consume/raise exceptions if there are any!
break

return fn, LifespanManager(asgi_app)
return fn, LifespanManager(asgi_app, state)


def wsgi_app_wrapper(wsgi_app, function_io_manager):
Expand Down

0 comments on commit 269fb4e

Please sign in to comment.