Skip to content
Myr

Simplify ASGI concurrency (#248)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Drop keyword argument * Improve docstrings for WSGIDispatch and ASGIDispatch * Add docs for fine grained WSGI/ASGI control * Simplify concurrency handling in ASGIDispatch * Variable renaming * Fix missing nonlocal declaration * Split nonlocal onto multiple lines

authored by Tom Christie · committed by GitHub · parent 79425b2 · Browse files · 1 file+10 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+10 added−12 removed

  • other:+10 added−12 removed

1 file+10 added−12 removed

modified:httpx/dispatch/asgi.py+10 added-12 removed
76 unmodified lines
77 app = self.app78 app_exc = None79 status_code = None80 headers = None81 response_started = asyncio.Event()81 response_started_or_failed = asyncio.Event()82 response_body = BodyIterator()83 request_stream = request.stream()8485 async def receive() -> dict:
5 unmodified lines
91 return {"type": "http.request", "body": b"", "more_body": False}92 return {"type": "http.request", "body": body, "more_body": True}9394 async def send(message: dict) -> None:95 nonlocal status_code, headers, response_started, response_body, request95 nonlocal status_code, headers, response_started_or_failed96 nonlocal response_body, request9798 if message["type"] == "http.response.start":99 status_code = message["status"]100 headers = message.get("headers", [])100 response_started.set()101 response_started_or_failed.set()102 elif message["type"] == "http.response.body":103 body = message.get("body", b"")104 more_body = message.get("more_body", False)105 if body and request.method != "HEAD":106 await response_body.put(body)107 if not more_body:107 await response_body.done()108 await response_body.mark_as_done()109110 async def run_app() -> None:111 nonlocal app, scope, receive, send, app_exc, response_body112 try:113 await app(scope, receive, send)114 except Exception as exc:115 app_exc = exc116 finally:116 await response_body.done()117 await response_body.mark_as_done()118 response_started_or_failed.set()119120 # Really we'd like to push all `asyncio` logic into concurrency.py,121 # with a standardized interface, so that we can support other event122 # loop implementations, such as Trio and Curio.123 # That's a bit fiddly here, so we're not yet supporting using a custom124 # `ConcurrencyBackend` with the `Client(app=asgi_app)` case.125 loop = asyncio.get_event_loop()126 app_task = loop.create_task(run_app())125 response_task = loop.create_task(response_started.wait())127127 tasks = {app_task, response_task} # type: typing.Set[asyncio.Task]128 await response_started_or_failed.wait()129129 await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)130130 if app_exc is not None and self.raise_app_exceptions:131 raise app_exc132134 assert response_started.is_set(), "application did not return a response."135 assert status_code is not None133 assert status_code is not None, "application did not return a response."134 assert headers is not None135136 async def on_close() -> None:137 nonlocal app_task, response_body
48 unmodified lines
186 Used by the server to add data to the response body.187 """188 await self._queue.put(data)189192 async def done(self) -> None:190 async def mark_as_done(self) -> None:191 """192 Used by the server to signal the end of the response body.193 """194 await self._queue.put(self._done)