Skip to content

Commit 04b42d7

Browse files
authored
Simplify loop handling in async Consul client (#93)
- Align with aiohttp's BaseConnector pattern of using `loop = loop or asyncio.get_running_loop()` - Remove redundant loop retrieval logic - Use `asyncio.get_running_loop()` instead of deprecated `asyncio.get_event_loop()` - Update documentation example to use `asyncio.get_running_loop()`
1 parent 3c0413f commit 04b42d7

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change log
22

3+
4+
## 1.5.5
5+
6+
- **fix:** Simplify loop handling in async Consul client
7+
8+
39
## 1.5.4
410

511
- **feature:** Improve robustness of JSON decoding

consul/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "1.5.4"
1+
__version__ = "1.5.5"
22

33
from consul.check import Check
44
from consul.exceptions import ACLDisabled, ACLPermissionDenied, ConsulException, NotFound, Timeout

consul/aio.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import asyncio
21
from typing import Dict, Optional
32

43
import aiohttp
@@ -13,11 +12,11 @@ class HTTPClient(base.HTTPClient):
1312

1413
def __init__(self, *args, loop=None, connections_limit=None, connections_timeout=None, **kwargs) -> None:
1514
super().__init__(*args, **kwargs)
16-
self._loop = loop or asyncio.get_event_loop()
15+
self.loop = loop
1716
connector_kwargs = {}
1817
if connections_limit:
1918
connector_kwargs["limit"] = connections_limit
20-
connector = aiohttp.TCPConnector(loop=self._loop, verify_ssl=self.verify, **connector_kwargs)
19+
connector = aiohttp.TCPConnector(loop=self.loop, verify_ssl=self.verify, **connector_kwargs)
2120
session_kwargs = {}
2221
if connections_timeout:
2322
timeout = aiohttp.ClientTimeout(total=connections_timeout)
@@ -76,7 +75,7 @@ def close(self):
7675

7776
class Consul(base.Consul):
7877
def __init__(self, *args, loop=None, connections_limit=None, connections_timeout=None, **kwargs) -> None:
79-
self._loop = loop or asyncio.get_event_loop()
78+
self.loop = loop
8079
self.connections_limit = connections_limit
8180
self.connections_timeout = connections_timeout
8281
super().__init__(*args, **kwargs)
@@ -86,7 +85,7 @@ def http_connect(self, host: str, port: int, scheme, verify: bool = True, cert=N
8685
host,
8786
port,
8887
scheme,
89-
loop=self._loop,
88+
loop=self.loop,
9089
connections_limit=self.connections_limit,
9190
connections_timeout=self.connections_timeout,
9291
verify=verify,

docs/index.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ result of each API call. This client is available in *consul.aio*.
5858
import consul.aio
5959
6060
61-
loop = asyncio.get_event_loop()
61+
loop = asyncio.get_running_loop()
6262
6363
@asyncio.coroutine
6464
def go():

0 commit comments

Comments
 (0)