Python GIL and multithreading

I would like to split the sigle-thread application by the number of worker threads. Just one question - how about doing this? If the GIL prevents python from executing more than one thread at a time, will I have any profit?

The other point (from the point of view of c / C ++) - as I know, each thread, in any case, can only be executed exclusively, therefore at a lower level than the python interpreter, I have the same restriction.

Summary: will python threads be less efficient than the native thread in task switching?

+4
source share
1 answer

Do not worry about GIL. Depending on what your program is doing (calculation versus I / O), you will have different performance characteristics. If your program is related to I / O, you probably won't notice the GIL at all.

Another approach is to use the multiprocessing module, where each process runs in its own OS process with its own Python runtime. You can take full advantage of multiple cores with this approach, and it is usually safer because you do not need to worry about synchronizing access to shared memory.

+5
source

All Articles