diff options
author | Adrian Herrmann <adrian.herrmann@qt.io> | 2024-03-05 00:30:44 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2024-03-07 23:38:27 +0000 |
commit | d6214ac026b4f4d9d34c8b9defc91c5a759845ff (patch) | |
tree | 31275158fdcc2c04ea0564365a1cefcf3516dbba | |
parent | 7bc27c4d62af36b74f445e213204cdd89d1fc771 (diff) |
QtAsyncio: Add handle_sigint argument to run()
An argument handle_sigint determines whether QtAsyncio should handle
SIGINT (Ctrl+C) and shut down the event loop when it is received. The
default is False. This can be set to True if you want QtAsyncio to take
care of handling SIGINT instead of your program.
Task-number: PYSIDE-769
Change-Id: Ie4364025448405f36158a8e997d90ae143961ba8
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
(cherry picked from commit dec0ac7a94c787d100d1ca3f9298b7c3b07712aa)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | sources/pyside6/PySide6/QtAsyncio/__init__.py | 4 | ||||
-rw-r--r-- | sources/pyside6/PySide6/QtAsyncio/events.py | 6 | ||||
-rw-r--r-- | sources/pyside6/doc/PySide6/QtAsyncio/index.rst | 5 |
3 files changed, 12 insertions, 3 deletions
diff --git a/sources/pyside6/PySide6/QtAsyncio/__init__.py b/sources/pyside6/PySide6/QtAsyncio/__init__.py index bb5cde9dd..599675f8e 100644 --- a/sources/pyside6/PySide6/QtAsyncio/__init__.py +++ b/sources/pyside6/PySide6/QtAsyncio/__init__.py @@ -20,6 +20,7 @@ __all__ = [ def run(coro: typing.Optional[typing.Coroutine] = None, keep_running: bool = True, quit_qapp: bool = True, *, + handle_sigint: bool = False, debug: typing.Optional[bool] = None) -> typing.Any: """Run the QtAsyncio event loop.""" @@ -32,7 +33,8 @@ 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 - asyncio.set_event_loop_policy(QAsyncioEventLoopPolicy(quit_qapp=quit_qapp)) + asyncio.set_event_loop_policy( + QAsyncioEventLoopPolicy(quit_qapp=quit_qapp, handle_sigint=handle_sigint)) if keep_running: if coro: diff --git a/sources/pyside6/PySide6/QtAsyncio/events.py b/sources/pyside6/PySide6/QtAsyncio/events.py index a19dc232a..bd58d7dba 100644 --- a/sources/pyside6/PySide6/QtAsyncio/events.py +++ b/sources/pyside6/PySide6/QtAsyncio/events.py @@ -59,7 +59,8 @@ class QAsyncioExecutorWrapper(QObject): class QAsyncioEventLoopPolicy(asyncio.AbstractEventLoopPolicy): def __init__(self, application: typing.Optional[QCoreApplication] = None, - quit_qapp: bool = True) -> None: + quit_qapp: bool = True, + handle_sigint: bool = False) -> None: super().__init__() if application is None: if QCoreApplication.instance() is None: @@ -70,7 +71,8 @@ class QAsyncioEventLoopPolicy(asyncio.AbstractEventLoopPolicy): self._quit_qapp = quit_qapp self._event_loop: typing.Optional[asyncio.AbstractEventLoop] = None - signal.signal(signal.SIGINT, signal.SIG_DFL) + if handle_sigint: + signal.signal(signal.SIGINT, signal.SIG_DFL) def get_event_loop(self) -> asyncio.AbstractEventLoop: if self._event_loop is None: diff --git a/sources/pyside6/doc/PySide6/QtAsyncio/index.rst b/sources/pyside6/doc/PySide6/QtAsyncio/index.rst index 90c0ee670..326f6efcc 100644 --- a/sources/pyside6/doc/PySide6/QtAsyncio/index.rst +++ b/sources/pyside6/doc/PySide6/QtAsyncio/index.rst @@ -131,6 +131,11 @@ coroutine has finished, while ``quit_qapp`` determines if the QCoreApplication should be shut down after asyncio has finished. It is possible for asyncio to finish while the QCoreApplication is kept alive. +An argument ``handle_sigint`` determines whether QtAsyncio should handle +SIGINT (Ctrl+C) and shut down the event loop when it is received. The +default is ``False``. Set this to ``True`` if you want QtAsyncio to take +care of handling SIGINT instead of your program. + Coroutines explained ^^^^^^^^^^^^^^^^^^^^ |