Skip to content

Commit d65fb3c

Browse files
committed
Wrapper and profiler for 3.4 coroutines
1 parent 71a61fb commit d65fb3c

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

_aio_34.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from asyncio import coroutine, iscoroutinefunction
2+
from contextlib import contextmanager
3+
from functools import wraps
4+
5+
from memory_profiler import LineProfiler, show_results
6+
7+
8+
class CoroLineProfiler(LineProfiler):
9+
@contextmanager
10+
def count_contextmgr(self):
11+
self.enable_by_count()
12+
try:
13+
yield
14+
finally:
15+
self.disable_by_count()
16+
17+
def wrap_function(self, func):
18+
if iscoroutinefunction(func):
19+
@coroutine
20+
def f(*args, **kwargs):
21+
with self.count_contextmgr():
22+
yield from func(*args, **kwargs)
23+
return f
24+
else:
25+
return super(CoroLineProfiler, self).wrap_function(func)
26+
27+
28+
def _get_coro_wrapper(coro, backend, stream, precision):
29+
@wraps(coro)
30+
@coroutine
31+
def wrapper(*args, **kwargs):
32+
prof = CoroLineProfiler(backend=backend)
33+
val = yield from prof(coro)(*args, **kwargs)
34+
show_results(prof, stream=stream, precision=precision)
35+
return val

0 commit comments

Comments
 (0)