mirror of
https://github.com/samsonjs/vdirsyncer.git
synced 2026-04-27 14:57:41 +00:00
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.
This commit is contained in:
parent
54e829262d
commit
cf1d082628
2 changed files with 52 additions and 63 deletions
|
|
@ -63,8 +63,5 @@ async def aio_session(event_loop):
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
async def aio_connector(event_loop):
|
async def aio_connector(event_loop):
|
||||||
conn = aiohttp.TCPConnector(limit_per_host=16)
|
async with aiohttp.TCPConnector(limit_per_host=16) as conn:
|
||||||
try:
|
|
||||||
yield conn
|
yield conn
|
||||||
finally:
|
|
||||||
await conn.close()
|
|
||||||
|
|
|
||||||
|
|
@ -127,8 +127,7 @@ def sync(ctx, collections, force_delete):
|
||||||
from .tasks import sync_collection
|
from .tasks import sync_collection
|
||||||
|
|
||||||
async def main(collections):
|
async def main(collections):
|
||||||
conn = aiohttp.TCPConnector(limit_per_host=16)
|
async with aiohttp.TCPConnector(limit_per_host=16) as conn:
|
||||||
|
|
||||||
tasks = []
|
tasks = []
|
||||||
for pair_name, collections in collections:
|
for pair_name, collections in collections:
|
||||||
async for collection, config in prepare_pair(
|
async for collection, config in prepare_pair(
|
||||||
|
|
@ -147,7 +146,6 @@ def sync(ctx, collections, force_delete):
|
||||||
)
|
)
|
||||||
|
|
||||||
await asyncio.gather(*tasks)
|
await asyncio.gather(*tasks)
|
||||||
await conn.close()
|
|
||||||
|
|
||||||
asyncio.run(main(collections))
|
asyncio.run(main(collections))
|
||||||
|
|
||||||
|
|
@ -166,7 +164,7 @@ def metasync(ctx, collections):
|
||||||
from .tasks import prepare_pair
|
from .tasks import prepare_pair
|
||||||
|
|
||||||
async def main(collections):
|
async def main(collections):
|
||||||
conn = aiohttp.TCPConnector(limit_per_host=16)
|
async with aiohttp.TCPConnector(limit_per_host=16) as conn:
|
||||||
|
|
||||||
for pair_name, collections in collections:
|
for pair_name, collections in collections:
|
||||||
collections = prepare_pair(
|
collections = prepare_pair(
|
||||||
|
|
@ -187,8 +185,6 @@ def metasync(ctx, collections):
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
await conn.close()
|
|
||||||
|
|
||||||
asyncio.run(main(collections))
|
asyncio.run(main(collections))
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -213,8 +209,7 @@ def discover(ctx, pairs, list):
|
||||||
config = ctx.config
|
config = ctx.config
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
conn = aiohttp.TCPConnector(limit_per_host=16)
|
async with aiohttp.TCPConnector(limit_per_host=16) as conn:
|
||||||
|
|
||||||
for pair_name in pairs or config.pairs:
|
for pair_name in pairs or config.pairs:
|
||||||
await discover_collections(
|
await discover_collections(
|
||||||
status_path=config.general["status_path"],
|
status_path=config.general["status_path"],
|
||||||
|
|
@ -224,8 +219,6 @@ def discover(ctx, pairs, list):
|
||||||
connector=conn,
|
connector=conn,
|
||||||
)
|
)
|
||||||
|
|
||||||
await conn.close()
|
|
||||||
|
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -267,14 +260,13 @@ def repair(ctx, collection, repair_unsafe_uid):
|
||||||
click.confirm("Do you want to continue?", abort=True)
|
click.confirm("Do you want to continue?", abort=True)
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
conn = aiohttp.TCPConnector(limit_per_host=16)
|
async with aiohttp.TCPConnector(limit_per_host=16) as conn:
|
||||||
await repair_collection(
|
await repair_collection(
|
||||||
ctx.config,
|
ctx.config,
|
||||||
collection,
|
collection,
|
||||||
repair_unsafe_uid=repair_unsafe_uid,
|
repair_unsafe_uid=repair_unsafe_uid,
|
||||||
connector=conn,
|
connector=conn,
|
||||||
)
|
)
|
||||||
await conn.close()
|
|
||||||
|
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue