Skip to content
Myr

Remove usage of loop.create_task in ASGIDispatch (#261)

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

* Remove usage of loop.create_task in ASGIDispatch * Add comment about usage of background manager * Update comment

authored by Florimond Manca · committed by GitHub · parent 3069227 · Browse files · 5 files+12 added−14 removed
This commit

State

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

Diff stats

5 files+12 added−14 removed

  • other:+12 added−14 removed

5 files+12 added−14 removed

modified:httpx/concurrency/asyncio.py+1 added-1 removed
221 unmodified lines
222 def create_event(self) -> BaseEvent:223 return typing.cast(BaseEvent, asyncio.Event())224225 def background_manager(226 self, coroutine: typing.Callable, args: typing.Any226 self, coroutine: typing.Callable, *args: typing.Any227 ) -> "BackgroundManager":228 return BackgroundManager(coroutine, args)229230
19 unmodified lines
modified:httpx/concurrency/base.py+1 added-1 removed
157 unmodified lines
158 def create_event(self) -> BaseEvent:159 raise NotImplementedError() # pragma: no cover160161 def background_manager(162 self, coroutine: typing.Callable, args: typing.Any162 self, coroutine: typing.Callable, *args: typing.Any163 ) -> "BaseBackgroundManager":164 raise NotImplementedError() # pragma: no cover165166
11 unmodified lines
modified:httpx/dispatch/asgi.py+8 added-10 removed
1import asyncio1import typing23from .base import AsyncDispatcher4from ..concurrency.base import ConcurrencyBackend
114 unmodified lines
119 finally:120 await response_body.mark_as_done()121 response_started_or_failed.set()122124 # Really we'd like to push all `asyncio` logic into concurrency.py,125 # with a standardized interface, so that we can support other event126 # loop implementations, such as Trio and Curio.127 # That's a bit fiddly here, so we're not yet supporting using a custom128 # `ConcurrencyBackend` with the `Client(app=asgi_app)` case.129 loop = asyncio.get_event_loop()130 app_task = loop.create_task(run_app())123 # Using the background manager here *works*, but it is weak design because124 # the background task isn't strictly context-managed.125 # We could consider refactoring the other uses of this abstraction126 # (mainly sending/receiving request/response data in h11 and h2 dispatchers),127 # and see if that allows us to come back here and refactor things out.128 background = await self.backend.background_manager(run_app).__aenter__()129130 await response_started_or_failed.wait()131132 if app_exc is not None and self.raise_app_exceptions:
2 unmodified lines
135 assert status_code is not None, "application did not return a response."136 assert headers is not None137138 async def on_close() -> None:141 nonlocal app_task, response_body139 nonlocal response_body140 await response_body.drain()143 await app_task141 await background.__aexit__(None, None, None)142 if app_exc is not None and self.raise_app_exceptions:143 raise app_exc144145 return AsyncResponse(
49 unmodified lines
modified:httpx/dispatch/http11.py+1 added-1 removed
43 unmodified lines
4445 await self._send_request(request, timeout)4647 task, args = self._send_request_data, [request.stream(), timeout]48 async with self.backend.background_manager(task, args=args):48 async with self.backend.background_manager(task, *args):49 http_version, status_code, headers = await self._receive_response(timeout)50 content = self._receive_response_data(timeout)5152 return AsyncResponse(
133 unmodified lines
modified:httpx/dispatch/http2.py+1 added-1 removed
39 unmodified lines
40 self.events[stream_id] = []41 self.timeout_flags[stream_id] = TimeoutFlag()4243 task, args = self.send_request_data, [stream_id, request.stream(), timeout]44 async with self.backend.background_manager(task, args=args):44 async with self.backend.background_manager(task, *args):45 status_code, headers = await self.receive_response(stream_id, timeout)46 content = self.body_iter(stream_id, timeout)47 on_close = functools.partial(self.response_closed, stream_id=stream_id)48
126 unmodified lines