You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It appears that memory_profiler does not produce any output at all if asked to profile a generator function (I couldn't find this documented anywhere, so I assume it's a bug)..
I took the simple example code snippet from https://pypi.python.org/pypi/memory_profiler and saved it as example.py. I then took a copy and modified it as follows and saved it to example2.py:
@profile
def my_func():
a = [1] * (10 ** 6)
b = [2] * (2 * 10 ** 7)
del b
yield a
if __name__ == '__main__':
next(my_func())
(i.e. replaced the "return" with a "yield" instead). I got the following results:
$ python3 -m memory_profiler example.py
Filename: example.py
Line # Mem usage Increment Line Contents
================================================
1 @profile
2 8.969 MB 0.000 MB def my_func():
3 16.699 MB 7.730 MB a = [1] * (10 ** 6)
4 169.324 MB 152.625 MB b = [2] * (2 * 10 ** 7)
5 16.738 MB -152.586 MB del b
6 16.738 MB 0.000 MB return a
$ python3 -m memory_profiler example2.py
$
Still see this problem with generators (not coroutines) both in py2 and py3 in my code. profile wrapper just outputs nothing
E.g. script
from memory_profiler import profile
@profile
def gen():
for i in range(100):
yield i
@profile
def func1():
return [i for i in gen()]
@profile
def func2():
return list(range(100))
assert func1() == func2()
Activity
fabianp commentedon Apr 6, 2013
yes that's a bug. Thanks for filling the issue.
Test issue pythonprofilers#42
Test issue pythonprofilers#42
jonasc commentedon Jul 19, 2016
I still see that it does not work for generator functions (version 0.41 from pip). Can it be fixed?
fabianp commentedon Jul 19, 2016
It should work, there are even some tests for this case. What is the error / failure?
jonasc commentedon Jul 19, 2016
Consider the following code:
Running it gives the following output (without any profiling output)
fabianp commentedon Jul 19, 2016
We don't support the new "yield from" syntax unfortunately. Patches welcome
:-)
(BTW don't import profile if you run with "-m memory_profiler", it can have
bad side effects)
On Tue, Jul 19, 2016 at 3:20 PM, jonasc notifications@github.com wrote:
astrojuanlu commentedon Oct 19, 2017
Change the title to "does not work with coroutines"? :)
yarikoptic commentedon Jun 7, 2018
what am I dong wrong?
wowkin2 commentedon Jul 28, 2018
Any progress on that?
delijati commentedon Apr 15, 2020
#255
Templarrr commentedon May 28, 2021
Still see this problem with generators (not coroutines) both in py2 and py3 in my code. profile wrapper just outputs nothing
E.g. script
outputs for func2 and func1but nothing for gen
luizoti commentedon Dec 12, 2022
The problem persistis, python 3.11.