Skip to content

Commit 4ea5072

Browse files
committed
Add slow_callback_duration logging
1 parent a6eb8e5 commit 4ea5072

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

qasync/__init__.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,13 @@ def __exit__(self, *args):
233233
self.shutdown()
234234

235235

236+
def _format_handle(handle: asyncio.Handle):
237+
cb = getattr(handle, "_callback", None)
238+
if isinstance(getattr(cb, '__self__', None), asyncio.tasks.Task):
239+
return repr(cb.__self__)
240+
return str(handle)
241+
242+
236243
def _make_signaller(qtimpl_qtcore, *args):
237244
class Signaller(qtimpl_qtcore.QObject):
238245
try:
@@ -275,8 +282,22 @@ def timerEvent(self, event): # noqa: N802
275282
if handle._cancelled:
276283
self.__log_debug("Handle %s cancelled", handle)
277284
else:
278-
self.__log_debug("Calling handle %s", handle)
279-
handle._run()
285+
if self.__debug_enabled:
286+
# This may not be the most efficient thing to do, but it removes the need to sync
287+
# "slow_callback_duration" and "_current_handle" variables
288+
loop = asyncio.get_event_loop()
289+
try:
290+
loop._current_handle = handle
291+
self._logger.debug("Calling handle %s", handle)
292+
t0 = time.time()
293+
handle._run()
294+
dt = time.time() - t0
295+
if dt >= loop.slow_callback_duration:
296+
self._logger.warning('Executing %s took %.3f seconds', _format_handle(handle), dt)
297+
finally:
298+
loop._current_handle = None
299+
else:
300+
handle._run()
280301
finally:
281302
del self.__callbacks[timerid]
282303
handle = None

0 commit comments

Comments
 (0)