Skip to content

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

Open

Description

@PveOnly

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

Activity

  1. bartbroere commented on Jun 13, 2024

    @bartbroere

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions