aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdrian Herrmann <adrian.herrmann@qt.io>2024-03-11 19:38:03 +0100
committerAdrian Herrmann <adrian.herrmann@qt.io>2024-03-11 20:42:52 +0100
commit463762e1e272ec79fadf9b8ae5a6fd25b7b8252e (patch)
treec583063f318cf70210834bdd174e7e3344be0aec
parent83c837969c8384d52b97318d310f9aa1705c6f0d (diff)
QtAsyncio: Reset loop policy after QtAsyncio.run()
When running QtAsyncio.run(), the global asyncio event loop policy is set. Currently, the policy setting is kept to QtAsyncio's policy even after QtAsyncio.run() returns, which can lead to unexpected behavior (e.g., if executing asyncio.run() afterwards expecting the default event loop to be used). Reset the event loop policy to the default one after returning from QtAsyncio.run() to mitigate this. Pick-to: 6.6 Task-number: PYSIDE-769 Change-Id: Ifd31c0924317ba09c53ded165c9a5d6f1e2dc808 Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
-rw-r--r--sources/pyside6/PySide6/QtAsyncio/__init__.py15
1 files changed, 13 insertions, 2 deletions
diff --git a/sources/pyside6/PySide6/QtAsyncio/__init__.py b/sources/pyside6/PySide6/QtAsyncio/__init__.py
index 599675f8e..4baa8134e 100644
--- a/sources/pyside6/PySide6/QtAsyncio/__init__.py
+++ b/sources/pyside6/PySide6/QtAsyncio/__init__.py
@@ -33,17 +33,28 @@ def run(coro: typing.Optional[typing.Coroutine] = None,
#
# More details:
# https://discuss.python.org/t/removing-the-asyncio-policy-system-asyncio-set-event-loop-policy-in-python-3-15/37553 # noqa: E501
+ default_policy = asyncio.get_event_loop_policy()
asyncio.set_event_loop_policy(
QAsyncioEventLoopPolicy(quit_qapp=quit_qapp, handle_sigint=handle_sigint))
+ ret = None
+ exc = None
+
if keep_running:
if coro:
asyncio.ensure_future(coro)
asyncio.get_event_loop().run_forever()
else:
if coro:
- return asyncio.run(coro, debug=debug)
+ ret = asyncio.run(coro, debug=debug)
else:
- raise RuntimeError(
+ exc = RuntimeError(
"QtAsyncio was set to keep running after the coroutine "
"finished, but no coroutine was provided.")
+
+ asyncio.set_event_loop_policy(default_policy)
+
+ if ret:
+ return ret
+ if exc:
+ raise exc