Add BaseQueue interface (#257)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Florimond Manca · committed by GitHub · parent e0b523d · Browse files · 3 files·+26 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+26 added−6 removed
- other:+26 added−6 removed
Provenance
- Commit
fc261e8- Tree
fb2cf1f- Author
Florimond Manca <florimond.manca@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
e0b523d
3 files changed+26 added−6 removed
modified:httpx/concurrency/asyncio.py+4 added
17 unmodified lines18 from ..exceptions import ConnectTimeout, PoolTimeout, ReadTimeout, WriteTimeout19 from ..interfaces import (20 BaseBackgroundManager,21 BasePoolSemaphore,22+ BaseQueue,23 BaseReader,24 BaseWriter,25 ConcurrencyBackend,26 )219 unmodified lines246 247 def get_semaphore(self, limits: PoolLimits) -> BasePoolSemaphore:248 return PoolSemaphore(limits)249 250+ def create_queue(self, max_size: int) -> BaseQueue:251+ return typing.cast(BaseQueue, asyncio.Queue(maxsize=max_size))252+253 def background_manager(254 self, coroutine: typing.Callable, args: typing.Any255 ) -> "BackgroundManager":256 return BackgroundManager(coroutine, args)21 unmodified linesmodified:httpx/dispatch/asgi.py+7 added-6 removed
1 import asyncio2 import typing3 4+from ..concurrency.asyncio import AsyncioBackend5 from ..config import CertTypes, TimeoutTypes, VerifyTypes5-from ..interfaces import AsyncDispatcher6+from ..interfaces import AsyncDispatcher, ConcurrencyBackend7 from ..models import AsyncRequest, AsyncResponse8 9 10 class ASGIDispatch(AsyncDispatcher):41 unmodified lines52 self.app = app53 self.raise_app_exceptions = raise_app_exceptions54 self.root_path = root_path55 self.client = client56+ # This will need to be turned into a parameter on this class at some point.57+ self.backend: ConcurrencyBackend = AsyncioBackend()58 59 async def send(60 self,61 request: AsyncRequest,19 unmodified lines81 app_exc = None82 status_code = None83 headers = None84 response_started_or_failed = asyncio.Event()82- response_body = BodyIterator()85+ response_body = BodyIterator(self.backend)86 request_stream = request.stream()87 88 async def receive() -> dict:89 nonlocal request_stream68 unmodified lines158 Provides a byte-iterator interface that the client can use to159 ingest the response content from.160 """161 159- def __init__(self) -> None:160- self._queue = asyncio.Queue(161- maxsize=1162- ) # type: asyncio.Queue[typing.Union[bytes, object]]162+ def __init__(self, backend: ConcurrencyBackend) -> None:163+ self._queue = backend.create_queue(max_size=1)164 self._done = object()165 166 async def iterate(self) -> typing.AsyncIterator[bytes]:167 """28 unmodified linesmodified:httpx/interfaces.py+15 added
143 unmodified lines144 async def close(self) -> None:145 raise NotImplementedError() # pragma: no cover146 147 148+class BaseQueue:149+ """150+ A FIFO queue. Abstracts away any asyncio-specific interfaces.151+ """152+153+ async def get(self) -> typing.Any:154+ raise NotImplementedError() # pragma: no cover155+156+ async def put(self, value: typing.Any) -> None:157+ raise NotImplementedError() # pragma: no cover158+159+160 class BasePoolSemaphore:161 """162 A semaphore for use with connection pooling.163 52 unmodified lines216 yield self.run(async_iterator.__anext__)217 except StopAsyncIteration:218 break219 220+ def create_queue(self, max_size: int) -> BaseQueue:221+ raise NotImplementedError() # pragma: no cover222+223 def background_manager(224 self, coroutine: typing.Callable, args: typing.Any225 ) -> "BaseBackgroundManager":226 raise NotImplementedError() # pragma: no cover13 unmodified lines