Skip to contentSkip to conversation

python: Replace asyncio.get_event_loop for Python 3.14

PATCH Superseded Richard W.M. Jones 3 participants 4 messages Jul 15, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 python: Replace asyncio.get_event_loop for Python 3.14

In Python 3.14, no asyncio event loop gets generated automatically. Instead create one when we need it. This should work with Python 3.13 as well. This is a mechanical change as suggested here:

https://bugzilla.redhat.com/show_bug.cgi?id=2375004#c4

See-also: https://docs.python.org/3.14/whatsnew/3.14.html#id7
Thanks: Miro Hrončok

---
 python/qemu/qmp/legacy.py  | 5 ++++-
 python/qemu/qmp/qmp_tui.py | 5 ++++-
 python/qemu/qmp/util.py    | 5 ++++-
 python/tests/protocol.py   | 5 ++++-
 4 files changed, 16 insertions(+), 4 deletions(-)
python/qemu/qmp/legacy.py +4 -1
index 22a2b5616e..e11d05afbd 100644
@@ -86,7 +86,10 @@ def __init__(self,
                 "server argument should be False when passing a socket")
 
         self._qmp = QMPClient(nickname)
-        self._aloop = asyncio.get_event_loop()
+        try:
+            self._aloop = asyncio.get_event_loop()
+        except RuntimeError:
+            self._aloop = asyncio.new_event_loop()
         self._address = address
         self._timeout: Optional[float] = None
 
python/qemu/qmp/qmp_tui.py +4 -1
index 2d9ebbd20b..84c5da4e66 100644
@@ -377,7 +377,10 @@ def run(self, debug: bool = False) -> None:
         screen = urwid.raw_display.Screen()
         screen.set_terminal_properties(256)
 
-        self.aloop = asyncio.get_event_loop()
+        try:
+            self.aloop = asyncio.get_event_loop()
+        except RuntimeError:
+            self.aloop = asyncio.new_event_loop()
         self.aloop.set_debug(debug)
 
         # Gracefully handle SIGTERM and SIGINT signals
python/qemu/qmp/util.py +4 -1
index ca6225e9cd..8f9f849a5f 100644
@@ -160,7 +160,10 @@ def asyncio_run(coro: Coroutine[Any, Any, T], *, debug: bool = False) -> T:
         return asyncio.run(coro, debug=debug)
 
     # Python 3.6
-    loop = asyncio.get_event_loop()
+    try:
+        loop = asyncio.get_event_loop()
+    except RuntimeError:
+        loop = asyncio.new_event_loop()
     loop.set_debug(debug)
     ret = loop.run_until_complete(coro)
     loop.close()
python/tests/protocol.py +4 -1
index 56c4d441f9..aa7c0e4397 100644
@@ -228,7 +228,10 @@ def async_test(async_test_method):
         Decorator; adds SetUp and TearDown to async tests.
         """
         async def _wrapper(self, *args, **kwargs):
-            loop = asyncio.get_event_loop()
+            try:
+                loop = asyncio.get_event_loop()
+            except RuntimeError:
+                loop = asyncio.new_event_loop()
             loop.set_debug(True)
 
             await self._asyncSetUp()
Signature · 1 line
2.50.1
#1 lore raw
Quoted 54 lines · Richard W.M. Jones

In Python 3.14, no asyncio event loop gets generated automatically. Instead create one when we need it. This should work with Python 3.13 as well. This is a mechanical change as suggested here:

https://bugzilla.redhat.com/show_bug.cgi?id=2375004#c4

See-also: https://docs.python.org/3.14/whatsnew/3.14.html#id7
Thanks: Miro Hrončok

---
  python/qemu/qmp/legacy.py  | 5 ++++-
  python/qemu/qmp/qmp_tui.py | 5 ++++-
  python/qemu/qmp/util.py    | 5 ++++-
  python/tests/protocol.py   | 5 ++++-
  4 files changed, 16 insertions(+), 4 deletions(-)
python/qemu/qmp/legacy.py +4 -1
index 22a2b5616e..e11d05afbd 100644
@@ -86,7 +86,10 @@ def __init__(self,
                  "server argument should be False when passing a socket")
 
          self._qmp = QMPClient(nickname)
-        self._aloop = asyncio.get_event_loop()
+        try:
+            self._aloop = asyncio.get_event_loop()
+        except RuntimeError:
+            self._aloop = asyncio.new_event_loop()
          self._address = address
          self._timeout: Optional[float] = None
 
python/qemu/qmp/qmp_tui.py +4 -1
index 2d9ebbd20b..84c5da4e66 100644
@@ -377,7 +377,10 @@ def run(self, debug: bool = False) -> None:
          screen = urwid.raw_display.Screen()
          screen.set_terminal_properties(256)
 
-        self.aloop = asyncio.get_event_loop()
+        try:
+            self.aloop = asyncio.get_event_loop()
+        except RuntimeError:
+            self.aloop = asyncio.new_event_loop()
          self.aloop.set_debug(debug)
 
          # Gracefully handle SIGTERM and SIGINT signals
python/qemu/qmp/util.py +0 -0
index ca6225e9cd..8f9f849a5f 100644
@@ -160,7 +160,10 @@ def asyncio_run(coro: Coroutine[Any, Any, T], *, debug: bool = False) -> T:
          return asyncio.run(coro, debug=debug)

python/qemu/qmp/util.py def asyncio_run(coro: Coroutine[Any, Any, T], *, debug: bool = False) -> T: · line 169

Quoted 10 lines
python/qemu/qmp/util.py +4 -1
 
      # Python 3.6
-    loop = asyncio.get_event_loop()
+    try:
+        loop = asyncio.get_event_loop()
+    except RuntimeError:
+        loop = asyncio.new_event_loop()
      loop.set_debug(debug)
      ret = loop.run_until_complete(coro)
      loop.close()

This one particular addition should not be necessary as it only runs on Python 3.6. This code is not reached on Python 3.7+ and hence it is not reached on Python 3.14.

Quoted 16 lines
python/tests/protocol.py +4 -1
index 56c4d441f9..aa7c0e4397 100644
@@ -228,7 +228,10 @@ def async_test(async_test_method):
          Decorator; adds SetUp and TearDown to async tests.
          """
          async def _wrapper(self, *args, **kwargs):
-            loop = asyncio.get_event_loop()
+            try:
+                loop = asyncio.get_event_loop()
+            except RuntimeError:
+                loop = asyncio.new_event_loop()
              loop.set_debug(True)
 
              await self._asyncSetUp()
Signature · 4 lines
Miro Hrončok
-- 
Phone: +420777974800
Fedora Matrix: mhroncok
#2 lore raw
Quoted 56 lines · Miro Hrončok
Quoted 55 lines · Richard W.M. Jones

In Python 3.14, no asyncio event loop gets generated automatically. Instead create one when we need it. This should work with Python 3.13 as well. This is a mechanical change as suggested here:

https://bugzilla.redhat.com/show_bug.cgi?id=2375004#c4

See-also: https://docs.python.org/3.14/whatsnew/3.14.html#id7
Thanks: Miro Hrončok

---
 python/qemu/qmp/legacy.py  | 5 ++++-
 python/qemu/qmp/qmp_tui.py | 5 ++++-
 python/qemu/qmp/util.py    | 5 ++++-
 python/tests/protocol.py   | 5 ++++-
 4 files changed, 16 insertions(+), 4 deletions(-)
python/qemu/qmp/legacy.py +4 -1
index 22a2b5616e..e11d05afbd 100644
@@ -86,7 +86,10 @@ def __init__(self,
                 "server argument should be False when passing a socket")
         self._qmp = QMPClient(nickname)
-        self._aloop = asyncio.get_event_loop()
+        try:
+            self._aloop = asyncio.get_event_loop()
+        except RuntimeError:
+            self._aloop = asyncio.new_event_loop()
         self._address = address
         self._timeout: Optional[float] = None
python/qemu/qmp/qmp_tui.py +4 -1
index 2d9ebbd20b..84c5da4e66 100644
@@ -377,7 +377,10 @@ def run(self, debug: bool = False) -> None:
         screen = urwid.raw_display.Screen()
         screen.set_terminal_properties(256)
-        self.aloop = asyncio.get_event_loop()
+        try:
+            self.aloop = asyncio.get_event_loop()
+        except RuntimeError:
+            self.aloop = asyncio.new_event_loop()
         self.aloop.set_debug(debug)
         # Gracefully handle SIGTERM and SIGINT signals
python/qemu/qmp/util.py +3 -1
index ca6225e9cd..8f9f849a5f 100644
@@ -160,7 +160,10 @@ def asyncio_run(coro: Coroutine[Any, Any, T], *, debug: bool = False) -> T:
         return asyncio.run(coro, debug=debug)
     # Python 3.6
-    loop = asyncio.get_event_loop()
+    try:
+        loop = asyncio.get_event_loop()
+    except RuntimeError:
Quoted 8 lines

python/qemu/qmp/util.py def asyncio_run(coro: Coroutine[Any, Any, T], *, debug: bool = False) -> T: · line 169

Quoted 4 lines
python/qemu/qmp/util.py +1 -0
+        loop = asyncio.new_event_loop()
     loop.set_debug(debug)
     ret = loop.run_until_complete(coro)
     loop.close()

This one particular addition should not be necessary as it only runs on Python 3.6. This code is not reached on Python 3.7+ and hence it is not reached on Python 3.14.

Yup, even says it in the comment :-)

I'll submit a v2 in a little while, in case there is any other feedback.

Rich.

Quoted 21 lines
Quoted 15 lines
python/tests/protocol.py +4 -1
index 56c4d441f9..aa7c0e4397 100644
@@ -228,7 +228,10 @@ def async_test(async_test_method):
         Decorator; adds SetUp and TearDown to async tests.
         """
         async def _wrapper(self, *args, **kwargs):
-            loop = asyncio.get_event_loop()
+            try:
+                loop = asyncio.get_event_loop()
+            except RuntimeError:
+                loop = asyncio.new_event_loop()
             loop.set_debug(True)
             await self._asyncSetUp()
Signature · 4 lines
Miro Hrončok
-- 
Phone: +420777974800
Fedora Matrix: mhroncok
Signature · 5 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
Fedora Windows cross-compiler. Compile Windows programs, test, and
build Windows installers. Over 100 libraries supported.
http://fedoraproject.org/wiki/MinGW
#3 lore raw
In reply to Richard W.M. Jones
Quoted 22 lines · Richard W.M. Jones

In Python 3.14, no asyncio event loop gets generated automatically. Instead create one when we need it. This should work with Python 3.13 as well. This is a mechanical change as suggested here:

https://bugzilla.redhat.com/show_bug.cgi?id=2375004#c4

See-also: https://docs.python.org/3.14/whatsnew/3.14.html#id7
Thanks: Miro Hrončok

---
 python/qemu/qmp/legacy.py  | 5 ++++-
 python/qemu/qmp/qmp_tui.py | 5 ++++-
 python/qemu/qmp/util.py    | 5 ++++-
 python/tests/protocol.py   | 5 ++++-
 4 files changed, 16 insertions(+), 4 deletions(-)
python/qemu/qmp/legacy.py +0 -0
index 22a2b5616e..e11d05afbd 100644
@@ -86,7 +86,10 @@ def __init__(self,
                 "server argument should be False when passing a socket")

python/qemu/qmp/legacy.py def __init__(self, · line 95

Quoted 10 lines
python/qemu/qmp/legacy.py +4 -1
 
         self._qmp = QMPClient(nickname)
-        self._aloop = asyncio.get_event_loop()
+        try:
+            self._aloop = asyncio.get_event_loop()
+        except RuntimeError:
+            self._aloop = asyncio.new_event_loop()
         self._address = address
         self._timeout: Optional[float] = None
 

The usage contract of QEMUMonitorProtocol is sufficiently ill defined that we probably have to expect the caller might have arranged for a specific event loop to be set against the thread, so the try/except dance is likely required.

Quoted 6 lines
python/qemu/qmp/qmp_tui.py +0 -0
index 2d9ebbd20b..84c5da4e66 100644
@@ -377,7 +377,10 @@ def run(self, debug: bool = False) -> None:
         screen = urwid.raw_display.Screen()

python/qemu/qmp/qmp_tui.py def run(self, debug: bool = False) -> None: · line 386

Quoted 10 lines
python/qemu/qmp/qmp_tui.py +4 -1
         screen.set_terminal_properties(256)
 
-        self.aloop = asyncio.get_event_loop()
+        try:
+            self.aloop = asyncio.get_event_loop()
+        except RuntimeError:
+            self.aloop = asyncio.new_event_loop()
         self.aloop.set_debug(debug)
 
         # Gracefully handle SIGTERM and SIGINT signals

I'm somewhat inclined to say that this "App" class should always use asyncio.new_event_loop, as the usge model is for running a self contained QMP application. I don't expect we should need to integrate with code that has set an event loop against the thread.

Quoted 6 lines
python/qemu/qmp/util.py +0 -0
index ca6225e9cd..8f9f849a5f 100644
@@ -160,7 +160,10 @@ def asyncio_run(coro: Coroutine[Any, Any, T], *, debug: bool = False) -> T:
         return asyncio.run(coro, debug=debug)

python/qemu/qmp/util.py def asyncio_run(coro: Coroutine[Any, Any, T], *, debug: bool = False) -> T: · line 169

Quoted 10 lines
python/qemu/qmp/util.py +4 -1
 
     # Python 3.6
-    loop = asyncio.get_event_loop()
+    try:
+        loop = asyncio.get_event_loop()
+    except RuntimeError:
+        loop = asyncio.new_event_loop()
     loop.set_debug(debug)
     ret = loop.run_until_complete(coro)
     loop.close()

This change is not requred as this code path only runs with Python <= 3.6. In fact it is dead code given we only support python >= 3.9.

I'm prepping a patch to kill all py 3.6 compat code.

Quoted 5 lines
python/tests/protocol.py +0 -0
index 56c4d441f9..aa7c0e4397 100644

python/tests/protocol.py def async_test(async_test_method): · line 235

Quoted 9 lines
python/tests/protocol.py +4 -1
@@ -228,7 +228,10 @@ def async_test(async_test_method):
         Decorator; adds SetUp and TearDown to async tests.
         """
         async def _wrapper(self, *args, **kwargs):
-            loop = asyncio.get_event_loop()
+            try:
+                loop = asyncio.get_event_loop()
+            except RuntimeError:
+                loop = asyncio.new_event_loop()
             loop.set_debug(True)

For the usage model here we should exclusively use asyncio.new_event_loop(). This is a test decorator so it is impossible for an existing event loop to be set against the thread.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|