I notice when I run my heavily CPU dependant python programs, it only uses a single core. Is it possible to assign multiple cores to the program when I run it?
Key Takeaways. Python is NOT a single-threaded language. Python processes typically use a single thread because of the GIL. Despite the GIL, libraries that perform computationally heavy tasks like numpy, scipy and pytorch utilise C-based implementations under the hood, allowing the use of multiple cores.
Multi-core processing can save a great deal of time when running large jobs or a large number of jobs. Due to the overhead involved in coordinating the flow of data between the cores, there is rarely a 1:1 ratio in the amount of time a program takes to run on multiple cores.
Core Settings In Windows 10Type 'msconfig' into the Windows Search Box and hit Enter. Select the Boot tab and then Advanced options. Check the box next to Number of processors and select the number of cores you want to use (probably 1, if you are having compatibility issues) from the menu. Select OK and then Apply.
You have to program explicitly for multiple cores. See the Symmetric Multiprocessing options on this page for the many parallel processing solutions in Python. Parallel Python is a good choice if you can't be bothered to compare the options, look at the examples here.
Some problems can't take advantage of multiple cores though. Think about how you could possibly run up the stairs faster with the help of three friends. Not going to happen!
I wonder why nobody mentioned CPython's GIL (Global Interpreter Lock) yet. It basically means that multiple threads inside one Python interpreter cannot use the power of multiple cores because many operations are protected by a global lock in order to be thread-safe. This only applies to a small amount of applications - the CPU-bound ones. For more info, just search for the term "GIL", there are already many questions on it (like that one, for example).
This answer of course assumes that you are in fact using multiple threads, or else you won't be able to use multiple cores anyway (multiprocessing would be another possibility).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With