aboutsummaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorJohn Snow <jsnow@redhat.com>2021-09-15 12:29:50 -0400
committerJohn Snow <jsnow@redhat.com>2021-09-27 12:10:29 -0400
commitdebbabd77f340758099212985dac75b3c1bedd47 (patch)
tree36c82abf45db2262da79644fe913ac8a85183260 /python
parent41f4f92260da1c45f6b68f5965a30e503f394269 (diff)
python/aqmp: add asyncio_run compatibility wrapper
As a convenience. It isn't used by the library itself, but it is used by the test suite. It will also come in handy for users of the library still on Python 3.6. Signed-off-by: John Snow <jsnow@redhat.com> Message-id: 20210915162955.333025-23-jsnow@redhat.com Signed-off-by: John Snow <jsnow@redhat.com>
Diffstat (limited to 'python')
-rw-r--r--python/qemu/aqmp/util.py19
1 files changed, 19 insertions, 0 deletions
diff --git a/python/qemu/aqmp/util.py b/python/qemu/aqmp/util.py
index 52a1532188..eaa5fc7d5f 100644
--- a/python/qemu/aqmp/util.py
+++ b/python/qemu/aqmp/util.py
@@ -147,6 +147,25 @@ async def wait_closed(writer: asyncio.StreamWriter) -> None:
await asyncio.sleep(0)
+def asyncio_run(coro: Coroutine[Any, Any, T], *, debug: bool = False) -> T:
+ """
+ Python 3.6-compatible `asyncio.run` wrapper.
+
+ :param coro: A coroutine to execute now.
+ :return: The return value from the coroutine.
+ """
+ if sys.version_info >= (3, 7):
+ return asyncio.run(coro, debug=debug)
+
+ # Python 3.6
+ loop = asyncio.get_event_loop()
+ loop.set_debug(debug)
+ ret = loop.run_until_complete(coro)
+ loop.close()
+
+ return ret
+
+
# ----------------------------
# Section: Logging & Debugging
# ----------------------------