I am doing a little bit of python multithreading programming and found the result of my code vary strange, not parallel at all (8 cores, 8 threads, 13% cpu utilization). Then I have found a python GIL term and these slides (http://www.dabeaz.com/python/GIL.pdf). Is it real that python is not parallelizable? Will the multiprocessing module help to utilize computational resources or there is another performance issue with that?
-
Which multithreading library did you use?Nebril– Nebril2014-12-24 10:26:45 +00:00Commented Dec 24, 2014 at 10:26
-
stackoverflow.com/questions/6821477/…NPE– NPE2014-12-24 10:28:00 +00:00Commented Dec 24, 2014 at 10:28
-
@Nebril I use standard threading module can you recommend something else. Because spawning new processes does not really fit me.itun– itun2014-12-24 10:30:05 +00:00Commented Dec 24, 2014 at 10:30
-
@itun as Reut wrote: try multiprocessing docs.python.org/2/library/multiprocessing.htmlNebril– Nebril2014-12-24 10:33:56 +00:00Commented Dec 24, 2014 at 10:33
-
1Praise the docs then!Nebril– Nebril2014-12-24 10:36:31 +00:00Commented Dec 24, 2014 at 10:36
1 Answer
From the docs:
CPython implementation detail: In CPython, due to the Global Interpreter Lock, only one thread can execute Python code at once (even though certain performance-oriented libraries might overcome this limitation). If you want your application to make better use of the computational resources of multi-core machines, you are advised to use multiprocessing. However, threading is still an appropriate model if you want to run multiple I/O-bound tasks simultaneously.
A a general rule of thumb:
When your bottleneck is I/O (like writing to disk...) - consider threading which allows the program to keep running elsewhere when something is blocking an execution path.
When your bottleneck is cpu power, consider multiprocessing which allows cpu utilization.