Skip to content

Commit c33afb0

Browse files
committed
Add coroutine support
1 parent 891d56e commit c33afb0

5 files changed

Lines changed: 79 additions & 45 deletions

File tree

_aio_34.py

Lines changed: 0 additions & 35 deletions
This file was deleted.

memory_profiler/__init__.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@
1717
import warnings
1818
import contextlib
1919

20-
from .common import PY2, HAS_TRACEMALLOC
20+
from .common import PY2, PY34, PY35, HAS_TRACEMALLOC
2121
from .utils import (
2222
show_results,
2323
choose_backend,
2424
get_memory as _get_memory,
2525
get_child_memory as _get_child_memory,
2626
)
27-
from .line_profiler import LineProfiler
27+
from .line_profiler import LineProfiler, get_profile_wrapper
28+
29+
if PY34:
30+
from ._aio_34 import get_profile_wrapper
31+
elif PY35:
32+
from ._aio_35 import get_profile_wrapper
2833

2934

3035
if sys.platform == "win32":
@@ -758,14 +763,7 @@ def profile(func=None, stream=None, precision=1, backend='psutil'):
758763
if not tracemalloc.is_tracing():
759764
tracemalloc.start()
760765
if func is not None:
761-
@wraps(func)
762-
def wrapper(*args, **kwargs):
763-
prof = LineProfiler(backend=backend)
764-
val = prof(func)(*args, **kwargs)
765-
show_results(prof, stream=stream, precision=precision)
766-
return val
767-
768-
return wrapper
766+
return get_profile_wrapper(func, precision, backend, stream)
769767
else:
770768
def inner_wrapper(f):
771769
return profile(f, stream=stream, precision=precision,

memory_profiler/_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 functools import wraps
3+
4+
from .line_profiler import (
5+
get_profile_wrapper as default_profile_wrapper,
6+
LineProfiler,
7+
)
8+
from .utils import show_results
9+
10+
11+
class CoroLineProfiler(LineProfiler):
12+
def wrap_function(self, func):
13+
if iscoroutinefunction(func):
14+
@coroutine
15+
def f(*args, **kwargs):
16+
with self.count_ctxmgr():
17+
yield from func(*args, **kwargs)
18+
return f
19+
else:
20+
return super().wrap_function(func)
21+
22+
23+
def get_profile_wrapper(func, precision, backend, stream):
24+
if iscoroutinefunction(func):
25+
@wraps(func)
26+
@coroutine
27+
def wrapper(*args, **kwargs):
28+
prof = CoroLineProfiler(backend=backend)
29+
val = yield from prof(func)(*args, **kwargs)
30+
show_results(prof, stream=stream, precision=precision)
31+
return val
32+
else:
33+
wrapper = default_profile_wrapper(func, precision, backend, stream)
34+
35+
return wrapper

memory_profiler/_aio_35.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from functools import wraps
2+
from inspect import iscoroutinefunction
3+
4+
from .line_profiler import (
5+
get_profile_wrapper as default_profile_wrapper,
6+
LineProfiler,
7+
)
8+
from .utils import show_results
9+
10+
11+
class CoroLineProfiler(LineProfiler):
12+
def wrap_function(self, func):
13+
if iscoroutinefunction(func):
14+
async def f(*args, **kwargs):
15+
with self.count_ctxmgr():
16+
return await func(*args, **kwargs)
17+
return f
18+
else:
19+
return super().wrap_function(func)
20+
21+
22+
def get_profile_wrapper(func, precision, backend, stream):
23+
if iscoroutinefunction(func):
24+
@wraps(func)
25+
async def wrapper(*args, **kwargs):
26+
prof = CoroLineProfiler(backend=backend)
27+
val = await prof(func)(*args, **kwargs)
28+
show_results(prof, stream=stream, precision=precision)
29+
return val
30+
else:
31+
wrapper = default_profile_wrapper(func, precision, backend, stream)
32+
33+
return wrapper

memory_profiler/common.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
PY2 = sys.version_info[0] == 2
55

6+
PY34 = (3, 4) < sys.version_info < (3, 5)
7+
PY35 = (3, 5) < sys.version_info
8+
69
try:
710
import tracemalloc # noqa
811
except ImportError:

0 commit comments

Comments
 (0)