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
Provenance
- Commit
80eafe8- Tree
ff02eed- Author
Florimond Manca <florimond.manca@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
3069227
5 files changed+12 added−14 removed
modified:httpx/concurrency/asyncio.py+1 added-1 removed
221 unmodified lines222 def create_event(self) -> BaseEvent:223 return typing.cast(BaseEvent, asyncio.Event())224 225 def background_manager(226- self, coroutine: typing.Callable, args: typing.Any226+ self, coroutine: typing.Callable, *args: typing.Any227 ) -> "BackgroundManager":228 return BackgroundManager(coroutine, args)229 230 19 unmodified linesmodified:httpx/concurrency/base.py+1 added-1 removed
157 unmodified lines158 def create_event(self) -> BaseEvent:159 raise NotImplementedError() # pragma: no cover160 161 def background_manager(162- self, coroutine: typing.Callable, args: typing.Any162+ self, coroutine: typing.Callable, *args: typing.Any163 ) -> "BaseBackgroundManager":164 raise NotImplementedError() # pragma: no cover165 166 11 unmodified linesmodified:httpx/dispatch/asgi.py+8 added-10 removed
1-import asyncio1 import typing2 3 from .base import AsyncDispatcher4 from ..concurrency.base import ConcurrencyBackend114 unmodified lines119 finally:120 await response_body.mark_as_done()121 response_started_or_failed.set()122 124- # 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__()129 130 await response_started_or_failed.wait()131 132 if app_exc is not None and self.raise_app_exceptions:2 unmodified lines135 assert status_code is not None, "application did not return a response."136 assert headers is not None137 138 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_exc144 145 return AsyncResponse(49 unmodified linesmodified:httpx/dispatch/http11.py+1 added-1 removed
43 unmodified lines44 45 await self._send_request(request, timeout)46 47 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)51 52 return AsyncResponse(133 unmodified linesmodified:httpx/dispatch/http2.py+1 added-1 removed
39 unmodified lines40 self.events[stream_id] = []41 self.timeout_flags[stream_id] = TimeoutFlag()42 43 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