summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio/events.py
diff options
context:
space:
mode:
authorAntoine Pitrou <pitrou@free.fr>2017-11-07 16:23:29 (GMT)
committerGitHub <noreply@github.com>2017-11-07 16:23:29 (GMT)
commit921e9432a1461bbf312c9c6dcc2b916be6c05fa0 (patch)
treefe7133dd95ad4d039de908df4af3bc71ef1f0bc8 /Lib/asyncio/events.py
parent1e5d54cfa031f1de9ee2d2e968e0551b6e2397b7 (diff)
downloadcpython-921e9432a1461bbf312c9c6dcc2b916be6c05fa0.zip
cpython-921e9432a1461bbf312c9c6dcc2b916be6c05fa0.tar.gz
cpython-921e9432a1461bbf312c9c6dcc2b916be6c05fa0.tar.bz2
bpo-31970: Reduce performance overhead of asyncio debug mode. (#4314)
* bpo-31970: Reduce performance overhead of asyncio debug mode.
Diffstat (limited to 'Lib/asyncio/events.py')
-rw-r--r--Lib/asyncio/events.py21
1 files changed, 20 insertions, 1 deletions
diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py
index 270a5e4..f2f2e28 100644
--- a/Lib/asyncio/events.py
+++ b/Lib/asyncio/events.py
@@ -19,6 +19,8 @@ import sys
import threading
import traceback
+from . import constants
+
def _get_function_source(func):
func = inspect.unwrap(func)
@@ -72,6 +74,23 @@ def _format_callback_source(func, args):
return func_repr
+def extract_stack(f=None, limit=None):
+ """Replacement for traceback.extract_stack() that only does the
+ necessary work for asyncio debug mode.
+ """
+ if f is None:
+ f = sys._getframe().f_back
+ if limit is None:
+ # Limit the amount of work to a reasonable amount, as extract_stack()
+ # can be called for each coroutine and future in debug mode.
+ limit = constants.DEBUG_STACK_DEPTH
+ stack = traceback.StackSummary.extract(traceback.walk_stack(f),
+ limit=limit,
+ lookup_lines=False)
+ stack.reverse()
+ return stack
+
+
class Handle:
"""Object returned by callback registration methods."""
@@ -85,7 +104,7 @@ class Handle:
self._cancelled = False
self._repr = None
if self._loop.get_debug():
- self._source_traceback = traceback.extract_stack(sys._getframe(1))
+ self._source_traceback = extract_stack(sys._getframe(1))
else:
self._source_traceback = None