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

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

Open
PveOnly opened this issue Jan 12, 2024 · 1 comment
Open

Comments

@PveOnly
Copy link

PveOnly commented Jan 12, 2024

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
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants