Search code examples
pythonmulticoregil

Would limiting a GILed Python program to a single CPU boost performance?


Following up on David Beazley's paper regarding Python and GIL, would it be a good practice to limit a Python program (CPython with GIL and all) to a single CPU in a Windows based multi-core system?

Would it boost performance?

UPDATE: Assume multiple threads are used (not sure if it makes a difference)


Solution

  • The paper does indeed imply that limiting a program to a single core would improve performance in that particular case. However, there are a number of concerns that you would need to deal with:

    1. His tests are mainly for compute intensive threads rather than IO bound threads. If the threads you are using often block voluntarily (such as in a web server waiting for a client) then you don't run into GIL issues at all.
    2. The GIL issues deal specifically with threads and not processes. I may be reading your question wrong, but you seem to be asking about restricting all Python programs to a single core. Programs using processes for parallelism don't suffer from GIL issues and restricting them to a single core will make them slower.
    3. The GIL is drastically different in Python 3.2 (as David mentions in this video. The GIL was changed explicitly to deal with such issues. While it still has problems, it no longer has this problem.

    In summary, the only time you want to complicate your life by forcing the OS to restrict the program to a single core is when you are running a:

    1. Multithreaded
    2. Compute Intensive
    3. Lower than Python 3.2

    program on a multicore machine.