diff options
author | Adrian Herrmann <adrian.herrmann@qt.io> | 2024-03-11 19:38:03 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2024-03-11 21:58:47 +0000 |
commit | f1b8986fa243425fbdf47be55227778def351416 (patch) | |
tree | 3e5e28c9279b95ff6868ce8f40383688e428ae3a | |
parent | c04fa31ccd6b29e88ceceeaad08f5db2dce0bcd2 (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.
Task-number: PYSIDE-769
Change-Id: Ifd31c0924317ba09c53ded165c9a5d6f1e2dc808
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
(cherry picked from commit 463762e1e272ec79fadf9b8ae5a6fd25b7b8252e)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | sources/pyside6/PySide6/QtAsyncio/__init__.py | 15 |
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 |