X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FN2VC.git;a=blobdiff_plain;f=modules%2Flibjuju%2Fjuju%2Futils.py;h=3565fd630c6a3dc3839fe3f382bdf5e1aab0fdc2;hp=1d1b24ecd647089ebb335a706350fa61dc29a52a;hb=bf79352ca652b228c5c216564cc512b635e3c5e4;hpb=68858c1915122c2dbc8999a5cd3229694abf5f3a diff --git a/modules/libjuju/juju/utils.py b/modules/libjuju/juju/utils.py index 1d1b24e..3565fd6 100644 --- a/modules/libjuju/juju/utils.py +++ b/modules/libjuju/juju/utils.py @@ -11,11 +11,11 @@ async def execute_process(*cmd, log=None, loop=None): ''' p = await asyncio.create_subprocess_exec( - *cmd, - stdin=asyncio.subprocess.PIPE, - stdout=asyncio.subprocess.PIPE, - stderr=asyncio.subprocess.PIPE, - loop=loop) + *cmd, + stdin=asyncio.subprocess.PIPE, + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + loop=loop) stdout, stderr = await p.communicate() if log: log.debug("Exec %s -> %d", cmd, p.returncode) @@ -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]