Is Python cStringIO thread safe?

As the title says, does Python cStringIO support its internal structures for multithreading?

Thank.

+3
source share
4 answers

Take a look at the excellent work on explaining GIL , then note that cStringIO is written exclusively in C, and its calls are not GIL releases.

This means that the current thread will not voluntarily switch during read () / write () (with the current virtual machine implementation). (The OS will preempt the thread, however other Python threads will not be able to get the GIL.)

Looking at the source: Python-2.7.1 / Modules / cStringIO.c does not mention the protection of internal components. If in doubt, look at the source :)

+5

, CPython Python.

CPython , , Python. , C, , .

, Python, cStringIO , , cStringIO , cStringIO . , C, , . , - , , , . seek, .

, , writelines, Python , , writelines.

Python: , , , , .

+3

It is currently not thread protected.

0
source

It is like "thread safe" because file operations can be (which means "little"). The Python implementation that you use is Global Interpreter Lock (GIL) , which ensures that every single file operation is cStringIOnot interrupted by another thread. This means that it does not guarantee that parallel operations with files from several streams will not be interleaved.

0
source

All Articles