Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add handling of asyncio coroutines. #162

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 41 additions & 16 deletions memory_profiler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""Profile the memory usage of a Python program"""

import asyncio
# .. we'll use this to pass it to the child script ..
import functools

_CLEAN_GLOBALS = globals().copy()

__version__ = '0.47'
Expand All @@ -16,7 +19,6 @@
import inspect
import subprocess
import logging
import traceback
from signal import SIGKILL


Expand Down Expand Up @@ -654,13 +656,24 @@ def wrap_function(self, func):
""" Wrap a function to profile it.
"""

def f(*args, **kwds):
self.enable_by_count()
try:
return func(*args, **kwds)
finally:
self.disable_by_count()

if not asyncio.iscoroutinefunction(func):
def f(*args, **kwds):
self.enable_by_count()
try:
return func(*args, **kwds)
finally:
self.disable_by_count()
else:
@asyncio.coroutine
def f(*args, **kwds):
self.enable_by_count()
try:
val = yield from func(*args, **kwds)
return val
finally:
self.disable_by_count()

functools.update_wrapper(f, func)
return f

def runctx(self, cmd, globals, locals):
Expand Down Expand Up @@ -1062,20 +1075,32 @@ def profile(func=None, stream=None, precision=1, backend='psutil'):
if backend == 'tracemalloc' and has_tracemalloc:
if not tracemalloc.is_tracing():
tracemalloc.start()

if func is not None:
def wrapper(*args, **kwargs):
prof = LineProfiler(backend=backend)
val = prof(func)(*args, **kwargs)
show_results(prof, stream=stream, precision=precision)
return val
if not asyncio.iscoroutinefunction(func):
def wrapper(*args, **kwargs):
prof = LineProfiler(backend=backend)
val = prof(func)(*args, **kwargs)
show_results(prof, stream=stream, precision=precision)

return val
else:
@asyncio.coroutine
def wrapper(*args, **kwargs):
prof = LineProfiler(backend=backend)
val = yield from prof(func)(*args, **kwargs)
show_results(prof, stream=stream, precision=precision)

return val

return wrapper
else:
def inner_wrapper(f):
def wrapper(f):
return profile(f, stream=stream, precision=precision,
backend=backend)

return inner_wrapper
functools.update_wrapper(wrapper, func)

return wrapper


def choose_backend(new_backend=None):
Expand Down
9 changes: 5 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import memory_profiler
import sys
from distutils.core import setup
import setuptools

import memory_profiler
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method to extract the version was changed.


CLASSIFIERS = """\
Development Status :: 5 - Production/Stable
Expand Down Expand Up @@ -31,6 +32,6 @@
py_modules=['memory_profiler'],
scripts=['mprof'],
classifiers=[_f for _f in CLASSIFIERS.split('\n') if _f],
license='BSD'

license='BSD',
install_requires=['asyncio>=3.4.3'] if sys.version_info < (3, 4) else [],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using PEP 426 markers instead? https://blog.ionelmc.ro/presentations/packaging/#slide:25

)