pythonprofilers / memory_profiler

Monitor Memory usage of Python code
http://pypi.python.org/pypi/memory_profiler
Other
4.39k stars 380 forks source link

Is there a specific reason memory_usage return None when stream=True ? #397

Open PveOnly opened 10 months ago

PveOnly commented 10 months ago

Hi, I'm just wondering why when stream is not None, memory_usage return None ? https://github.com/pythonprofilers/memory_profiler/blob/a99a3c3b3c2eb01c90f6e14ddfcb85b3e97f9885/memory_profiler.py#L478C6-L478C6

I'm using stream + retval so my script is crashing since i'm getting a None object when doing mem_usage,res=memory_usage((func,args,kwargs),stream=True, retval=True

My current fix is just removing the if stream : return None, but I would like to know if it will bite me later or if it's fine ? I'm really not familiar with memory usage so maybe there is a specific reason. Thanks

bartbroere commented 5 months ago

Is there a specific reason memory_usage return None when stream=True ?

@PveOnly Looking at the method you describe, I think stream is not intended to receive a boolean, but instead a file or a file-like object.

stream : File if stream is a File opened with write access, then results are written to this file instead of stored in memory and returned at the end of the subprocess. Useful for long-running processes. Implies timestamps=True.

So if you supply something as the stream parameter, it will try to write output there, instead of returning it.

In your example that could be something like:

with open('outputfile', 'w') as filehandle:
    mem_usage, res = memory_usage((func,args,kwargs), stream=filehandle, retval=True)