Skip to content

Commit cf1d082

Browse files
author
Hugo Osvaldo Barrera
committed
Use context managers for aio connectors
Not sure why we didn't do this initially, but this ensures that we always close all connectors properly, and also gives much clearer scope regarding their life-cycles.
1 parent 54e8292 commit cf1d082

File tree

2 files changed

+53
-64
lines changed

2 files changed

+53
-64
lines changed

tests/conftest.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,5 @@ async def aio_session(event_loop):
6363

6464
@pytest.fixture
6565
async def aio_connector(event_loop):
66-
conn = aiohttp.TCPConnector(limit_per_host=16)
67-
try:
66+
async with aiohttp.TCPConnector(limit_per_host=16) as conn:
6867
yield conn
69-
finally:
70-
await conn.close()

vdirsyncer/cli/__init__.py

Lines changed: 52 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -127,27 +127,25 @@ def sync(ctx, collections, force_delete):
127127
from .tasks import sync_collection
128128

129129
async def main(collections):
130-
conn = aiohttp.TCPConnector(limit_per_host=16)
131-
132-
tasks = []
133-
for pair_name, collections in collections:
134-
async for collection, config in prepare_pair(
135-
pair_name=pair_name,
136-
collections=collections,
137-
config=ctx.config,
138-
connector=conn,
139-
):
140-
tasks.append(
141-
sync_collection(
142-
collection=collection,
143-
general=config,
144-
force_delete=force_delete,
145-
connector=conn,
130+
async with aiohttp.TCPConnector(limit_per_host=16) as conn:
131+
tasks = []
132+
for pair_name, collections in collections:
133+
async for collection, config in prepare_pair(
134+
pair_name=pair_name,
135+
collections=collections,
136+
config=ctx.config,
137+
connector=conn,
138+
):
139+
tasks.append(
140+
sync_collection(
141+
collection=collection,
142+
general=config,
143+
force_delete=force_delete,
144+
connector=conn,
145+
)
146146
)
147-
)
148147

149-
await asyncio.gather(*tasks)
150-
await conn.close()
148+
await asyncio.gather(*tasks)
151149

152150
asyncio.run(main(collections))
153151

@@ -166,28 +164,26 @@ def metasync(ctx, collections):
166164
from .tasks import prepare_pair
167165

168166
async def main(collections):
169-
conn = aiohttp.TCPConnector(limit_per_host=16)
170-
171-
for pair_name, collections in collections:
172-
collections = prepare_pair(
173-
pair_name=pair_name,
174-
collections=collections,
175-
config=ctx.config,
176-
connector=conn,
177-
)
178-
179-
await asyncio.gather(
180-
*[
181-
metasync_collection(
182-
collection=collection,
183-
general=config,
184-
connector=conn,
185-
)
186-
async for collection, config in collections
187-
]
188-
)
167+
async with aiohttp.TCPConnector(limit_per_host=16) as conn:
168+
169+
for pair_name, collections in collections:
170+
collections = prepare_pair(
171+
pair_name=pair_name,
172+
collections=collections,
173+
config=ctx.config,
174+
connector=conn,
175+
)
189176

190-
await conn.close()
177+
await asyncio.gather(
178+
*[
179+
metasync_collection(
180+
collection=collection,
181+
general=config,
182+
connector=conn,
183+
)
184+
async for collection, config in collections
185+
]
186+
)
191187

192188
asyncio.run(main(collections))
193189

@@ -213,18 +209,15 @@ def discover(ctx, pairs, list):
213209
config = ctx.config
214210

215211
async def main():
216-
conn = aiohttp.TCPConnector(limit_per_host=16)
217-
218-
for pair_name in pairs or config.pairs:
219-
await discover_collections(
220-
status_path=config.general["status_path"],
221-
pair=config.get_pair(pair_name),
222-
from_cache=False,
223-
list_collections=list,
224-
connector=conn,
225-
)
226-
227-
await conn.close()
212+
async with aiohttp.TCPConnector(limit_per_host=16) as conn:
213+
for pair_name in pairs or config.pairs:
214+
await discover_collections(
215+
status_path=config.general["status_path"],
216+
pair=config.get_pair(pair_name),
217+
from_cache=False,
218+
list_collections=list,
219+
connector=conn,
220+
)
228221

229222
asyncio.run(main())
230223

@@ -267,14 +260,13 @@ def repair(ctx, collection, repair_unsafe_uid):
267260
click.confirm("Do you want to continue?", abort=True)
268261

269262
async def main():
270-
conn = aiohttp.TCPConnector(limit_per_host=16)
271-
await repair_collection(
272-
ctx.config,
273-
collection,
274-
repair_unsafe_uid=repair_unsafe_uid,
275-
connector=conn,
276-
)
277-
await conn.close()
263+
async with aiohttp.TCPConnector(limit_per_host=16) as conn:
264+
await repair_collection(
265+
ctx.config,
266+
collection,
267+
repair_unsafe_uid=repair_unsafe_uid,
268+
connector=conn,
269+
)
278270

279271
asyncio.run(main())
280272

0 commit comments

Comments
 (0)