Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Python cStringIO thread-safe?

As title say, does Python cStringIO protect their internal structures for multithreading use?

Thank you.

like image 559
Emilio Avatar asked Jan 19 '23 18:01

Emilio


1 Answers

Take a look at an excellent work on explaining GIL, then note that cStringIO is written purely in C, and its calls don't release GIL.

It means that the running thread won't voluntarily switch during read()/write() (with current virtual machine implementation). (The OS will preempt the thread, however other Python threads won't be able to acquire GIL.)

Taking a look at the source: Python-2.7.1/Modules/cStringIO.c there is no mention about internals protection. When in doubt, look at source :)

like image 120
janislaw Avatar answered Jan 22 '23 09:01

janislaw