diff -r d428b4ee8b39 Lib/asyncio/base_events.py --- a/Lib/asyncio/base_events.py Fri Jan 09 16:00:30 2015 +0100 +++ b/Lib/asyncio/base_events.py Fri Jan 09 16:28:42 2015 +0100 @@ -179,6 +179,7 @@ class BaseEventLoop(events.AbstractEvent # In debug mode, if the execution of a callback or a step of a task # exceed this duration in seconds, the slow callback/task is logged. self.slow_callback_duration = 0.1 + self._current_handle = None def __repr__(self): return ('<%s running=%s closed=%s debug=%s>' @@ -932,6 +933,10 @@ class BaseEventLoop(events.AbstractEvent else: exc_info = False + if (self._current_handle is not None + and self._current_handle._source_traceback): + context['handle_traceback'] = self._current_handle._source_traceback + log_lines = [message] for key in sorted(context): if key in {'message', 'exception'}: @@ -941,6 +946,10 @@ class BaseEventLoop(events.AbstractEvent tb = ''.join(traceback.format_list(value)) value = 'Object created at (most recent call last):\n' value += tb.rstrip() + elif key == 'handle_traceback': + tb = ''.join(traceback.format_list(value)) + value = 'Handle created at (most recent call last):\n' + value += tb.rstrip() else: value = repr(value) log_lines.append('{}: {}'.format(key, value)) @@ -1098,12 +1107,16 @@ class BaseEventLoop(events.AbstractEvent if handle._cancelled: continue if self._debug: - t0 = self.time() - handle._run() - dt = self.time() - t0 - if dt >= self.slow_callback_duration: - logger.warning('Executing %s took %.3f seconds', - _format_handle(handle), dt) + try: + self._current_handle = handle + t0 = self.time() + handle._run() + dt = self.time() - t0 + if dt >= self.slow_callback_duration: + logger.warning('Executing %s took %.3f seconds', + _format_handle(handle), dt) + finally: + self._current_handle = None else: handle._run() handle = None # Needed to break cycles when an exception occurs.