From 8e48b44ff902f9562ae58e4a1390146635f60a3f Mon Sep 17 00:00:00 2001 From: Fergus Mitchell Date: Thu, 21 Jul 2022 14:40:32 +0100 Subject: [PATCH 1/3] Allow Channel objects to be created outside of async contexts --- grpclib/client.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/grpclib/client.py b/grpclib/client.py index abf4a67..525668e 100644 --- a/grpclib/client.py +++ b/grpclib/client.py @@ -670,7 +670,7 @@ def __init__( self._host = host self._port = port - self._loop = loop or asyncio.get_event_loop() + self._loop = loop self._path = path self._codec = codec self._status_details_codec = status_details_codec @@ -703,6 +703,8 @@ def _protocol_factory(self) -> H2Protocol: return H2Protocol(Handler(), self._config, self._h2_config) async def _create_connection(self) -> H2Protocol: + if not self._loop: + self._loop = asyncio.get_running_loop() if self._path is not None: _, protocol = await self._loop.create_unix_connection( self._protocol_factory, self._path, ssl=self._ssl, From 88c0aab8a7ca3b7657dfae584d2f7b336459ded3 Mon Sep 17 00:00:00 2001 From: Fergus Mitchell Date: Fri, 22 Jul 2022 14:57:17 +0100 Subject: [PATCH 2/3] use local variables to satisfy mypy --- grpclib/client.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/grpclib/client.py b/grpclib/client.py index 525668e..f1a4013 100644 --- a/grpclib/client.py +++ b/grpclib/client.py @@ -703,14 +703,13 @@ def _protocol_factory(self) -> H2Protocol: return H2Protocol(Handler(), self._config, self._h2_config) async def _create_connection(self) -> H2Protocol: - if not self._loop: - self._loop = asyncio.get_running_loop() + loop = self._loop or asyncio.get_running_loop() if self._path is not None: - _, protocol = await self._loop.create_unix_connection( + _, protocol = await loop.create_unix_connection( self._protocol_factory, self._path, ssl=self._ssl, ) else: - _, protocol = await self._loop.create_connection( + _, protocol = await loop.create_connection( self._protocol_factory, self._host, self._port, ssl=self._ssl, ) @@ -794,11 +793,15 @@ def __del__(self) -> None: if self._protocol is not None: message = 'Unclosed connection: {!r}'.format(self) warnings.warn(message, ResourceWarning) - if self._loop.is_closed(): + try: + loop = self._loop or asyncio.get_running_loop() + except RuntimeError: + # There is no loop to handle exception on. return - else: - self.close() - self._loop.call_exception_handler({'message': message}) + if loop.is_closed(): + return + self.close() + loop.call_exception_handler({'message': message}) async def __aenter__(self) -> 'Channel': return self From 2da4ee60e97072cccd0ba0aff6a55d63ddec3124 Mon Sep 17 00:00:00 2001 From: Fergus Mitchell Date: Fri, 22 Jul 2022 15:02:59 +0100 Subject: [PATCH 3/3] slight refactor of previous commit --- grpclib/client.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/grpclib/client.py b/grpclib/client.py index f1a4013..87e2d2d 100644 --- a/grpclib/client.py +++ b/grpclib/client.py @@ -798,10 +798,9 @@ def __del__(self) -> None: except RuntimeError: # There is no loop to handle exception on. return - if loop.is_closed(): - return - self.close() - loop.call_exception_handler({'message': message}) + if not loop.is_closed(): + self.close() + loop.call_exception_handler({'message': message}) async def __aenter__(self) -> 'Channel': return self