summaryrefslogtreecommitdiff
path: root/Lib/asyncio/events.py
diff options
context:
space:
mode:
authorAntoine Pitrou <pitrou@free.fr>2017-11-07 17:50:48 +0100
committerGitHub <noreply@github.com>2017-11-07 17:50:48 +0100
commitd8d218ffda6b7569625ff9edadbbc9a2b1055e32 (patch)
tree1c0346361bddaaf5febd26429f0464ee94b1a237 /Lib/asyncio/events.py
parent518c6b97868d9c665475a40567b0aa417afad607 (diff)
downloadcpython-git-d8d218ffda6b7569625ff9edadbbc9a2b1055e32.tar.gz
[3.6] bpo-31970: Reduce performance overhead of asyncio debug mode. (GH-4314) (#4322)
* bpo-31970: Reduce performance overhead of asyncio debug mode.. (cherry picked from commit 921e9432a1461bbf312c9c6dcc2b916be6c05fa0)
Diffstat (limited to 'Lib/asyncio/events.py')
-rw-r--r--Lib/asyncio/events.py22
1 files changed, 20 insertions, 2 deletions
diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py
index d41f3f5b75..05dc8969f4 100644
--- a/Lib/asyncio/events.py
+++ b/Lib/asyncio/events.py
@@ -19,7 +19,8 @@ import sys
import threading
import traceback
-from asyncio import compat
+from . import compat
+from . import constants
def _get_function_source(func):
@@ -77,6 +78,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."""
@@ -90,7 +108,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