Skip to content
Myr

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 file

Take it

Close outstanding connections on pool.close()Tom Christiebefore adoption799df23

line 1why, one step back
1import asyncio

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 2–5why, one step back
2import typing
3from types import TracebackType45from .config import SSLConfig, TimeoutConfig

Module renamingTom Christiebefore adoptionb7d315a

line 6why, one step back
6from .connectionpool import ConnectionPool

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 7–10why, one step back
7from .datastructures import URL, Client, Response
8910class SyncResponse:

Close outstanding connections on pool.close()Tom Christiebefore adoption799df23

line 11why, one step back
11 def __init__(self, response: Response, loop: asyncio.AbstractEventLoop):

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

line 12why, one step back
12 self._response = response

Close outstanding connections on pool.close()Tom Christiebefore adoption799df23

line 13why, one step back
13 self._loop = loop

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 14–31why, one step back
14
15 @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 back
32 return self._loop.run_until_complete(self._response.read())

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 33–37why, one step back
33
34 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 back
38 yield self._loop.run_until_complete(inner.__anext__())

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 39–42why, one step back
39 except StopAsyncIteration as exc:
40 break4142 def close(self) -> None:

Close outstanding connections on pool.close()Tom Christiebefore adoption799df23

line 43why, one step back
43 return self._loop.run_until_complete(self._response.close())

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 44–48why, one step back
44
4546class SyncClient:47 def __init__(self, client: Client):48 self._client = client

Close outstanding connections on pool.close()Tom Christiebefore adoption799df23

line 49why, one step back
49 self._loop = asyncio.new_event_loop()

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 50–61why, one step back
50
51 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 back
62 response = self._loop.run_until_complete(

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 63–72why, one step back
63 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 back
73 return SyncResponse(response, self._loop)

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 74–75why, one step back
74
75 def close(self) -> None:

Close outstanding connections on pool.close()Tom Christiebefore adoption799df23

line 76why, one step back
76 self._loop.run_until_complete(self._client.close())

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 77–92why, one step back
77
78 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)