X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FN2VC.git;a=blobdiff_plain;f=modules%2Flibjuju%2Fjuju%2Futils.py;h=3565fd630c6a3dc3839fe3f382bdf5e1aab0fdc2;hp=1d9bc1cc4cc84a6c132e23661e2a307fb4cbe860;hb=29ad6453fb8cdece73b8c2f623cf81d5d730982d;hpb=1a15d1c84fc826fa7996c1c9d221a324edd33432;ds=sidebyside diff --git a/modules/libjuju/juju/utils.py b/modules/libjuju/juju/utils.py index 1d9bc1c..3565fd6 100644 --- a/modules/libjuju/juju/utils.py +++ b/modules/libjuju/juju/utils.py @@ -46,6 +46,7 @@ async def read_ssh_key(loop): can be passed on to a model. ''' + loop = loop or asyncio.get_event_loop() return await loop.run_in_executor(None, _read_ssh_key) @@ -71,6 +72,16 @@ class IdQueue: await queue.put(value) +async def block_until(*conditions, timeout=None, wait_period=0.5, loop=None): + """Return only after all conditions are true. + + """ + async def _block(): + while not all(c() for c in conditions): + await asyncio.sleep(wait_period, loop=loop) + await asyncio.wait_for(_block(), timeout, loop=loop) + + async def run_with_interrupt(task, event, loop=None): """ Awaits a task while allowing it to be interrupted by an `asyncio.Event`. @@ -91,6 +102,10 @@ async def run_with_interrupt(task, event, loop=None): return_when=asyncio.FIRST_COMPLETED) for f in pending: f.cancel() + exception = [f.exception() for f in done + if f is not event_task and f.exception()] + if exception: + raise exception[0] result = [f.result() for f in done if f is not event_task] if result: return result[0]