sync.py
This file
Path
httpcore/sync.py
92 lines · 2.5 KiB · Python
Last changed by
Close outstanding connections on pool.close()Tom Christiebefore adoption799df23
History of this fileTake it
Close outstanding connections on pool.close()Tom Christiebefore adoption799df23
line 1why, one step back1import asyncioMerge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 2–5why, one step back2import typing3from types import TracebackType45from .config import SSLConfig, TimeoutConfigModule renamingTom Christiebefore adoptionb7d315a
line 6why, one step back6from .connectionpool import ConnectionPoolMerge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 7–10why, one step back7from .datastructures import URL, Client, Response8910class SyncResponse:Close outstanding connections on pool.close()Tom Christiebefore adoption799df23
line 11why, one step back11 def __init__(self, response: Response, loop: asyncio.AbstractEventLoop):Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
line 12why, one step back12 self._response = responseClose outstanding connections on pool.close()Tom Christiebefore adoption799df23
line 13why, one step back13 self._loop = loopMerge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 14–31why, one step back1415 @property16 def status_code(self) -> int:17 return self._response.status_code1819 @property20 def reason(self) -> str:21 return self._response.reason2223 @property24 def headers(self) -> typing.List[typing.Tuple[bytes, bytes]]:25 return self._response.headers2627 @property28 def body(self) -> bytes:29 return self._response.body3031 def read(self) -> bytes:Close outstanding connections on pool.close()Tom Christiebefore adoption799df23
line 32why, one step back32 return self._loop.run_until_complete(self._response.read())Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 33–37why, one step back3334 def stream(self) -> typing.Iterator[bytes]:35 inner = self._response.stream()36 while True:37 try:Close outstanding connections on pool.close()Tom Christiebefore adoption799df23
line 38why, one step back38 yield self._loop.run_until_complete(inner.__anext__())Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 39–42why, one step back39 except StopAsyncIteration as exc:40 break4142 def close(self) -> None:Close outstanding connections on pool.close()Tom Christiebefore adoption799df23
line 43why, one step back43 return self._loop.run_until_complete(self._response.close())Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 44–48why, one step back444546class SyncClient:47 def __init__(self, client: Client):48 self._client = clientClose outstanding connections on pool.close()Tom Christiebefore adoption799df23
line 49why, one step back49 self._loop = asyncio.new_event_loop()Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 50–61why, one step back5051 def request(52 self,53 method: str,54 url: typing.Union[str, URL],55 *,56 headers: typing.Sequence[typing.Tuple[bytes, bytes]] = (),57 body: typing.Union[bytes, typing.AsyncIterator[bytes]] = b"",58 ssl: typing.Optional[SSLConfig] = None,59 timeout: typing.Optional[TimeoutConfig] = None,60 stream: bool = False,61 ) -> SyncResponse:Close outstanding connections on pool.close()Tom Christiebefore adoption799df23
line 62why, one step back62 response = self._loop.run_until_complete(Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 63–72why, one step back63 self._client.request(64 method,65 url,66 headers=headers,67 body=body,68 ssl=ssl,69 timeout=timeout,70 stream=stream,71 )72 )Close outstanding connections on pool.close()Tom Christiebefore adoption799df23
line 73why, one step back73 return SyncResponse(response, self._loop)Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 74–75why, one step back7475 def close(self) -> None:Close outstanding connections on pool.close()Tom Christiebefore adoption799df23
line 76why, one step back76 self._loop.run_until_complete(self._client.close())Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 77–92why, one step back7778 def __enter__(self) -> "SyncClient":79 return self8081 def __exit__(82 self,83 exc_type: typing.Type[BaseException] = None,84 exc_value: BaseException = None,85 traceback: TracebackType = None,86 ) -> None:87 self.close()888990def SyncConnectionPool(*args: typing.Any, **kwargs: typing.Any) -> SyncClient:91 client = ConnectionPool(*args, **kwargs) # type: ignore92 return SyncClient(client)