Till Schulte
Till Schulte

Reputation: 43

Is it possible to delete the bytes io.BytesIO after they've been read from memory?

I want to use the BytesIO class from io to create a data stream, but if I pipe big masses of data through it, it uses much memory, so I'm asking if its possible to free the memory used by "old" data I already read.

If this isn't possible with the io module, I am open to other solutions to my problem.

Here are is the way its implemented right now:

def __init__():
        self.audio: av.container.InputContainer = av.open(
            stream, "r", timeout=8, options=options, format="webm"
        )
        self.audio_stream: av.audio.stream.AudioStream = self.audio.streams.get(
            audio=0
        )[0]

        self.output_buffer: io.BytesIO = io.BytesIO(b"")
        self.output_container: av.container.OutputContainer = av.open(
            self.output_buffer, "w", format="opus"
        )

        self.output_stream: av.audio.stream.AudioStream = self.output_container.add_stream(
            "libopus", 48000
        )

...


def fill(self):
    position = 0
    for packet in self.audio.demux(self.audio_stream):
        packet: av.Packet
        position += packet.duration
        self.output_buffer.seek(0, 2)
        self.output_container.mux_one(packet)

...

# then you can read from the buffer with
b = self.output_buffer.read(bytes_to_read)
# this should be Opus Encoded Audio data in bytes format

Thank you in advance!

Upvotes: 2

Views: 2629

Answers (1)

Till Schulte
Till Schulte

Reputation: 43

For everyone still reading, I found a solution working for me. With python's os module you can create a new pipe (os.pipe()), which handles all these tasks for you.

Upvotes: 1

Related Questions