Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9b524d5

Browse files
committed
Issue #23208, asyncio: Add BaseEventLoop._current_handle
In debug mode, BaseEventLoop._run_once() now sets the BaseEventLoop._current_handle attribute to the handle currently executed. In release mode or when no handle is executed, the attribute is None. BaseEventLoop.default_exception_handler() displays the traceback of the current handle if available.
1 parent 3d2256f commit 9b524d5

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

Lib/asyncio/base_events.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ def __init__(self):
179179
# In debug mode, if the execution of a callback or a step of a task
180180
# exceed this duration in seconds, the slow callback/task is logged.
181181
self.slow_callback_duration = 0.1
182+
self._current_handle = None
182183

183184
def __repr__(self):
184185
return ('<%s running=%s closed=%s debug=%s>'
@@ -955,6 +956,10 @@ def default_exception_handler(self, context):
955956
else:
956957
exc_info = False
957958

959+
if (self._current_handle is not None
960+
and self._current_handle._source_traceback):
961+
context['handle_traceback'] = self._current_handle._source_traceback
962+
958963
log_lines = [message]
959964
for key in sorted(context):
960965
if key in {'message', 'exception'}:
@@ -964,6 +969,10 @@ def default_exception_handler(self, context):
964969
tb = ''.join(traceback.format_list(value))
965970
value = 'Object created at (most recent call last):\n'
966971
value += tb.rstrip()
972+
elif key == 'handle_traceback':
973+
tb = ''.join(traceback.format_list(value))
974+
value = 'Handle created at (most recent call last):\n'
975+
value += tb.rstrip()
967976
else:
968977
value = repr(value)
969978
log_lines.append('{}: {}'.format(key, value))
@@ -1121,12 +1130,16 @@ def _run_once(self):
11211130
if handle._cancelled:
11221131
continue
11231132
if self._debug:
1124-
t0 = self.time()
1125-
handle._run()
1126-
dt = self.time() - t0
1127-
if dt >= self.slow_callback_duration:
1128-
logger.warning('Executing %s took %.3f seconds',
1129-
_format_handle(handle), dt)
1133+
try:
1134+
self._current_handle = handle
1135+
t0 = self.time()
1136+
handle._run()
1137+
dt = self.time() - t0
1138+
if dt >= self.slow_callback_duration:
1139+
logger.warning('Executing %s took %.3f seconds',
1140+
_format_handle(handle), dt)
1141+
finally:
1142+
self._current_handle = None
11301143
else:
11311144
handle._run()
11321145
handle = None # Needed to break cycles when an exception occurs.

0 commit comments

Comments
 (0)