Skip to content

Commit

Permalink
Clean up to asgi.py
Browse files Browse the repository at this point in the history
Remove some logs that Mike pointed out, also don't always raise a runtime error
and remove fastapi import
  • Loading branch information
hoodmane committed Jan 7, 2025
1 parent 4485b18 commit a8e5ecf
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/pyodide/internal/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from contextlib import contextmanager
from inspect import isawaitable

from fastapi import Depends, Request

ASGI = {"spec_version": "2.0", "version": "3.0"}

Expand All @@ -16,11 +15,6 @@ def run_in_background(coro):
fut.add_done_callback(background_tasks.discard)


@Depends
async def env(request: Request):
return request.scope["env"]


@contextmanager
def acquire_js_buffer(pybuffer):
from pyodide.ffi import create_proxy
Expand Down Expand Up @@ -84,11 +78,10 @@ async def receive():

async def send(got):
if got["type"] == "lifespan.startup.complete":
print("Application startup complete.")
print("Uvicorn running")
ready.set_result(None)
return
if got["type"] == "lifespan.shutdown.complete":
print("Application shutdown complete")
return
raise RuntimeError(f"Unexpected lifespan event {got['type']}")

run_in_background(
Expand Down Expand Up @@ -217,3 +210,16 @@ async def fetch(app, req, env):

async def websocket(app, req):
return await process_websocket(app, req)


def __getattr__(name):
if name == "env":
from fastapi import Depends, Request

@Depends
async def env(request: Request):
return request.scope["env"]

return env

raise AttributeError(f"module {__name__!r} has no attribute {name!r}")

0 comments on commit a8e5ecf

Please sign in to comment.