Skip to content

Commit f3127cd

Browse files
committed
Add support for using QEventLoop inside of QThread
1 parent a8e4873 commit f3127cd

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

qasync/__init__.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@
100100
from ._common import with_logger # noqa
101101

102102

103+
_AllEvents = QtCore.QEventLoop.ProcessEventsFlags(0x00)
104+
105+
103106
@with_logger
104107
class _QThreadWorker(QtCore.QThread):
105108
"""
@@ -234,7 +237,7 @@ def __exit__(self, *args):
234237

235238
def _format_handle(handle: asyncio.Handle):
236239
cb = getattr(handle, "_callback", None)
237-
if isinstance(getattr(cb, '__self__', None), asyncio.tasks.Task):
240+
if isinstance(getattr(cb, "__self__", None), asyncio.tasks.Task):
238241
return repr(cb.__self__)
239242
return str(handle)
240243

@@ -292,7 +295,11 @@ def timerEvent(self, event): # noqa: N802
292295
handle._run()
293296
dt = time.time() - t0
294297
if dt >= loop.slow_callback_duration:
295-
self._logger.warning('Executing %s took %.3f seconds', _format_handle(handle), dt)
298+
self._logger.warning(
299+
"Executing %s took %.3f seconds",
300+
_format_handle(handle),
301+
dt,
302+
)
296303
finally:
297304
loop._current_handle = None
298305
else:
@@ -338,7 +345,7 @@ class _QEventLoop:
338345
... await asyncio.sleep(.1)
339346
>>>
340347
>>> asyncio.run(xplusy(2, 2), loop_factory=lambda:QEventLoop(app))
341-
348+
342349
If the event loop shall be used with an existing and already running QApplication
343350
it must be specified in the constructor via already_running=True
344351
In this case the user is responsible for loop cleanup with stop() and close()
@@ -420,7 +427,9 @@ def stop(*args):
420427
self.run_forever()
421428
finally:
422429
future.remove_done_callback(stop)
423-
self.__app.processEvents() # run loop one last time to process all the events
430+
self.__app.eventDispatcher().processEvents(
431+
_AllEvents
432+
) # run loop one last time to process all the events
424433
if not future.done():
425434
raise RuntimeError("Event loop stopped before Future completed.")
426435

0 commit comments

Comments
 (0)