简体   繁体   中英

Creating a buffer in python to store data before sending it out

I receive an mp3 stream in client1 and then I send it out again to client2 in real time.
I want to create a buffer which would store some data and it would only send it out to client2 when it fills completely. This would help me to avoid deadlocks.

I am not sure how to create such a buffer in python. What data structure can I use and how can I implement it? Any hints?

One of the ways I consider idiomatic and pythonic is using a list and .append() each buffer element/string to the list. When I'm done and I want the whole buffer, I do "".join(buffer_list) and get the whole shebang. It works like a StringBuilder

You could also look into StringIO and the C-extended version.

This page has a lot of info on the subject: Efficient String Concatenation in Python

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM