Search code examples
pythonprocessorgil

Is a python processed pinned to one CPU, or can it uses multiple CPU overtime?


So I know that even multithreaded python process can not use multiple core at the same time. But, by default, does that mean a python process is "pinned" to one CPU? By pinned, I mean, will the python process use always the same CPU, or can the same process, use overtime the different CPU of my machine?


Solution

  • By default, a python process is not pinned to a particular CPU core. In fact, despite the GIL, a single python process can spawn multiple threads -- each of which can be scheduled simultaneously by the OS on different CPU cores. Although the GIL makes it difficult for more than one thread to actually make progress at any given time (since they must all contend for the lock), even this can happen (native code can release the GIL unless / until it needs to access Python datastructures).

    You can, of course, use your operating system utilities to pin any process (including Python) to a specific CPU core.