Skip to content

Commit

Permalink
Use context managers for aio connectors
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Hugo Osvaldo Barrera committed Aug 16, 2021
1 parent 54e8292 commit cf1d082
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 64 deletions.
5 changes: 1 addition & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,5 @@ async def aio_session(event_loop):

@pytest.fixture
async def aio_connector(event_loop):
conn = aiohttp.TCPConnector(limit_per_host=16)
try:
async with aiohttp.TCPConnector(limit_per_host=16) as conn:
yield conn
finally:
await conn.close()
112 changes: 52 additions & 60 deletions vdirsyncer/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,27 +127,25 @@ def sync(ctx, collections, force_delete):
from .tasks import sync_collection

async def main(collections):
conn = aiohttp.TCPConnector(limit_per_host=16)

tasks = []
for pair_name, collections in collections:
async for collection, config in prepare_pair(
pair_name=pair_name,
collections=collections,
config=ctx.config,
connector=conn,
):
tasks.append(
sync_collection(
collection=collection,
general=config,
force_delete=force_delete,
connector=conn,
async with aiohttp.TCPConnector(limit_per_host=16) as conn:
tasks = []
for pair_name, collections in collections:
async for collection, config in prepare_pair(
pair_name=pair_name,
collections=collections,
config=ctx.config,
connector=conn,
):
tasks.append(
sync_collection(
collection=collection,
general=config,
force_delete=force_delete,
connector=conn,
)
)
)

await asyncio.gather(*tasks)
await conn.close()
await asyncio.gather(*tasks)

asyncio.run(main(collections))

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

async def main(collections):
conn = aiohttp.TCPConnector(limit_per_host=16)

for pair_name, collections in collections:
collections = prepare_pair(
pair_name=pair_name,
collections=collections,
config=ctx.config,
connector=conn,
)

await asyncio.gather(
*[
metasync_collection(
collection=collection,
general=config,
connector=conn,
)
async for collection, config in collections
]
)
async with aiohttp.TCPConnector(limit_per_host=16) as conn:

for pair_name, collections in collections:
collections = prepare_pair(
pair_name=pair_name,
collections=collections,
config=ctx.config,
connector=conn,
)

await conn.close()
await asyncio.gather(
*[
metasync_collection(
collection=collection,
general=config,
connector=conn,
)
async for collection, config in collections
]
)

asyncio.run(main(collections))

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

async def main():
conn = aiohttp.TCPConnector(limit_per_host=16)

for pair_name in pairs or config.pairs:
await discover_collections(
status_path=config.general["status_path"],
pair=config.get_pair(pair_name),
from_cache=False,
list_collections=list,
connector=conn,
)

await conn.close()
async with aiohttp.TCPConnector(limit_per_host=16) as conn:
for pair_name in pairs or config.pairs:
await discover_collections(
status_path=config.general["status_path"],
pair=config.get_pair(pair_name),
from_cache=False,
list_collections=list,
connector=conn,
)

asyncio.run(main())

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

async def main():
conn = aiohttp.TCPConnector(limit_per_host=16)
await repair_collection(
ctx.config,
collection,
repair_unsafe_uid=repair_unsafe_uid,
connector=conn,
)
await conn.close()
async with aiohttp.TCPConnector(limit_per_host=16) as conn:
await repair_collection(
ctx.config,
collection,
repair_unsafe_uid=repair_unsafe_uid,
connector=conn,
)

asyncio.run(main())

Expand Down

0 comments on commit cf1d082

Please sign in to comment.