Search code examples
pythonmultithreadingslowdown

python thread using only half of available cpu


I am working on a large computational problem in python. Running the problem linearly pegs one of my cpus to 100%. So I paralleled it using the threading library. The results were not so good. All of my cpus are being used, but they seem to be all maxing out at 50% utilization. Overall performance was worse than the linear program. Since my threads are all purely computational and independent, shouldn't they be taking up nearly 100% of all the cpus? Why are they running up to a 50% ceiling? Is this something built in to python?


Solution

  • Python has a Global Interpreter Lock which means that only one Python thread will be actively running at any given time - multiple threads allow various threads to be sleeping while another is active, but they won't be active simultaneously. You may want to look at the multiprocessing module instead which uses multiple interpreter processes to work around the GIL, since each process has its own GIL.