From 9a6c4b5b30d3492792be94a904a227389a02a4fb Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 15 May 2020 15:07:41 +0300 Subject: [PATCH] Fix asyncio event loop init in qml_bridge.py Depending on Python version the file may be imported in a thread and asyncio only implicitly creates an event loop in the main thread of the process. Backend does things which need asyncio so we must ensure an event loop exists before it is imported. Fixes #15 --- src/backend/qml_bridge.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/backend/qml_bridge.py b/src/backend/qml_bridge.py index 52e453b6..ee311fd2 100644 --- a/src/backend/qml_bridge.py +++ b/src/backend/qml_bridge.py @@ -40,12 +40,16 @@ class QMLBridge: """ def __init__(self) -> None: + try: + self._loop = asyncio.get_event_loop() + except RuntimeError: + self._loop = asyncio.new_event_loop() + asyncio.set_event_loop(self._loop) + self._loop.set_exception_handler(self._loop_exception_handler) + from .backend import Backend self.backend: Backend = Backend() - self._loop = asyncio.get_event_loop() - self._loop.set_exception_handler(self._loop_exception_handler) - Thread(target=self._start_asyncio_loop).start()