Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When are Python threads fast?

We're all aware of the horrors of the GIL, and I've seen a lot of discussion about the right time to use the multiprocessing module, but I still don't feel that I have a good intuition about when threading in Python (focusing mainly on CPython) is the right answer.

What are instances in which the GIL is not a significant bottleneck? What are the types of use cases where threading is the most appropriate answer?

like image 781
mvanveen Avatar asked Jan 24 '12 21:01

mvanveen


1 Answers

The GIL prevents python from running multiple threads.

If your code releases the GIL before jumping into a C extension, other python threads can continue while the C code runs. Like with the blocking IO, that other people have mentioned.

Ctypes does this automatically, and so does numpy. So if your code uses them a lot, it may not be significantly restricted by the GIL.

like image 138
mdaoust Avatar answered Oct 10 '22 16:10

mdaoust