From c46edb13b1c51acaa3daaeba6b01221166881af1 Mon Sep 17 00:00:00 2001 From: veractor <56646254+veractor@users.noreply.github.com> Date: Sat, 15 Jul 2023 15:03:34 +0100 Subject: [PATCH] Only init already_running QEventLoops as running When a QEventLoop is not already running, it should not set itself as the running loop on construction, and should do so exclusively when actually ran, i.e. in run_forever. Already running QEventLoops OTOH should always set themselves as the running loop on construction. This renders the set_running_loop constructor parameter obsolete. It is kept for compatibility. --- qasync/__init__.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/qasync/__init__.py b/qasync/__init__.py index 165c5c6..f971a8b 100644 --- a/qasync/__init__.py +++ b/qasync/__init__.py @@ -330,9 +330,11 @@ class _QEventLoop: If the event loop shall be used with an existing and already running QApplication it must be specified in the constructor via already_running=True In this case the user is responsible for loop cleanup with stop() and close() + + The set_running_loop parameter is there for backwards compatibility and does nothing. """ - def __init__(self, app=None, set_running_loop=True, already_running=False): + def __init__(self, app=None, set_running_loop=False, already_running=False): self.__app = app or QApplication.instance() assert self.__app is not None, "No QApplication has been instantiated" self.__is_running = False @@ -350,9 +352,6 @@ def __init__(self, app=None, set_running_loop=True, already_running=False): assert self.__app is not None super().__init__() - if set_running_loop: - asyncio.events._set_running_loop(self) - # We have to set __is_running to True after calling # super().__init__() because of a bug in BaseEventLoop. if already_running: @@ -363,6 +362,9 @@ def __init__(self, app=None, set_running_loop=True, already_running=False): self._before_run_forever() self.__app.aboutToQuit.connect(self._after_run_forever) + # for asyncio to recognize the already running loop + asyncio.events._set_running_loop(self) + def run_forever(self): """Run eventloop forever."""