I am running a python program in a server having python2.7.6 . I have used threading
module of python to create multiple threads . I have created 23 threads , so i am confused whether all my processor cores are actually being used or not , Is there any way i can check this in my server . Any suggestion as what should be the ideal number of threads that should be spawned according to the number of processors that we have in order to improve efficiency of my program.

- 490
- 2
- 15

- 1,850
- 1
- 25
- 45
2 Answers
David Beazly has a great talk on threading in Python. He also has a great presentation on the subject here.
Unfortunately, Python has something called the GIL which limits Python to executing a single thread at a time. To use all your cores you'd have to use multiple processes: See Multiprocessing.
Some in the Python community don't necessarily look at the GIL as a setback, you can utilize multiple cores through other means than shared-memory threading.
Look here for a great blog post on utilizing multiple cores in Python.
Edit: The above is true for CPython (the most common and also the reference implementation of Python). There's a few "yes but" answers out there (mostly referring to multithreading on a different Python implementation) but I'd generally point people to answers like this one that describe the GIL in CPython and alternatives for utilizing multiple cores6
-
Thanks for answering but My program initially used to take around 14 minutes to complete . However after applying threading it has been reduced to 3 minutes . So definitely i think multiple cores are getting utilized as the time has reduced drastically . – kkk Feb 11 '16 at 04:28
-
@kkk - if you review the links i posted you should be well-versed in multi-threading in Python. Multi-threading can have performance increases when your process is IO intensive, since a thread will release it's lock on the GIL while it's busy waiting for the IO resources. If your application is CPU intensive; you will see little or no performance improvement from using multiple threads. You can even see a severe performance penalty! – nlloyd Feb 11 '16 at 04:33
There is no real answer to this question and everyone might have a different view. Determining the number of threads for your application to improve performance should be decided after testing for x
scenarios with y
of threads. Performance depends on how the OS scheduler is scheduling your threads to the available CPU cores depending on the CPU load or number of processes running. If you have 4 cores, then it doesn't mean it's good to execute 4 threads only. In fact you can run 23 threads too. This is what we call the illusion of parallelism given to us by the scheduler by scheduling processes after processes hence making us think everything is running simultaneously.
Here is the thing
If you run 1 thread, you may not gain enough performance. As you keep increasing threads to infinity, then the scheduler will take more time to schedule threads and hamper your overall application performance.

- 2,853
- 1
- 25
- 44
-
Yeah true that , the complete process is taking comparatively lesser time but can you tell me as how the threads are getting allocated to different processors while using threading module ?? – kkk Feb 11 '16 at 04:39
-
We can't. The kernel allocates it based on different algorithms. However once a thread gets a CPU, the OS tries to allocate the thread to that CPU always called as the locality of reference – ffff Feb 11 '16 at 04:42
-
okk got that , but are the threads being allocated to different processors when using threading module of python. – kkk Feb 11 '16 at 05:35
-
Allocation is independent of any modules. Modules are to abstract thread creation. Allocating to them is purely the work of scheduler and works the same for any language – ffff Feb 11 '16 at 05:45