How efficient is threading in Python?

后端 未结 6 2105
故里飘歌
故里飘歌 2020-12-16 00:44

I heard threading is not very efficient in Python (compared to other languages).

Is this true? If so, how can a Python programmer overcome this?

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-16 01:20

    CPython uses reference counting with a cyclic garbage collector for memory management. To make this practical, it has a mechanism called the "global interpreter lock" which protects the reference counting system, along with all the other interpreter internals.

    On a single-core machine, this doesn't matter - all threading is faked via time-slicing, anyway. On a multiple core machine, it makes a difference: a CPU bound Python program running on CPython won't make use of all of the available cores.

    There are a number of possible responses to this:

    • use multiple processes instead of multiple threads (also opens up future scalability to multiple machines, rather than different cores within a single machine)
    • use a Python implementation with a more multicore friendly garbage collection mechanism (such as Jython, IronPython or PyPy)
    • move the more intensive CPU operations out to C code and release the GIL while doing the calculations (thus allowing the C code to run on other cores even though only a single Python thread is active at any one time)

    If threads are being used to convert blocking IO to a non-blocking operation, then that works just fine in standard CPython without any special modifications - IO operations already release the GIL.

提交回复
热议问题