23

I've written a buffer class that provides a File-like interface with read, write, seek, tell, flush methods to a simple string in memory. Of course it is incomplete (e.g. I didn't write readline). It's purpose is to be filled by a background thread from some external data source, but let a user treat it like a file. I'd expect it to contain a relatively small amount of data (maybe 50K max)

Is there a better way to do this instead of writing it from scratch?

2 Answers 2

33

You can use the standard Python modules StringIO or cStringIO to obtain an in-memory buffer which implements the file interface.

cStringIO is implemented in C, and will be faster, so you should use that version if possible.

If you're using Python 3 you should use the io.StringIO instead of StringIO and io.BytesIO instead of cStringIO.

Sign up to request clarification or add additional context in comments.

4 Comments

I think that's what I'm looking for. But are these classes thread-safe? I will have separate reader and writer threads.
Pythons GIL means that thread safety is highly unlikely to be a problem, as two threads don't execute at the same time.
That doesn't make it thread-safe. If it did, then we wouldn't need thread safety on single-core processors.
Thanks to @Sean1708 for including the Python3 notes:)
7

I think you might be looking for StringIO.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.