aboutsummaryrefslogtreecommitdiffstats
path: root/cloud_mdir_sync/util.py
diff options
context:
space:
mode:
authorJason Gunthorpe <jgg@mellanox.com>2020-02-07 11:52:43 -0400
committerJason Gunthorpe <jgg@mellanox.com>2020-02-07 12:11:03 -0400
commitd1877aaf5a791204dd7222f50fc33b5f802c7e94 (patch)
tree8c6db52a7656bc99da9e196913de95953771fbc5 /cloud_mdir_sync/util.py
parentf44a42f276e01514a1e4bf3028639aaea58138c6 (diff)
downloadcloud_mdir_sync-d1877aaf5a791204dd7222f50fc33b5f802c7e94.tar.gz
cloud_mdir_sync-d1877aaf5a791204dd7222f50fc33b5f802c7e94.tar.bz2
cloud_mdir_sync-d1877aaf5a791204dd7222f50fc33b5f802c7e94.zip
Don't leave asyncio tasks running unexpectedly
All cases where gather is called intend that the tasks will complete successfully or all cancel at the first error. Add a little wrapper to achieve this. Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
Diffstat (limited to 'cloud_mdir_sync/util.py')
-rw-r--r--cloud_mdir_sync/util.py13
1 files changed, 13 insertions, 0 deletions
diff --git a/cloud_mdir_sync/util.py b/cloud_mdir_sync/util.py
index 7799356..df8ce59 100644
--- a/cloud_mdir_sync/util.py
+++ b/cloud_mdir_sync/util.py
@@ -1,4 +1,5 @@
# SPDX-License-Identifier: GPL-2.0+
+import asyncio
import contextlib
import functools
import inspect
@@ -68,3 +69,15 @@ def sizeof_fmt(num, suffix='B'):
def pj(json_dict):
print(json.dumps(json_dict, indent=4, sort_keys=True))
+
+
+async def asyncio_complete(*awo_list):
+ """This is like asyncio.gather but it always ensures that the list of
+ awaitable objects is completed upon return. For instance if an exception
+ is thrown then all the awaitables are canceled"""
+ g = asyncio.gather(*awo_list)
+ try:
+ await g
+ finally:
+ g.cancel()
+ await asyncio.gather(*awo_list, return_exceptions=True)