Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How efficient is threading in Python?

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?

like image 895
webnat0 Avatar asked Feb 26 '11 16:02

webnat0


2 Answers

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.

like image 87
ncoghlan Avatar answered Oct 11 '22 22:10

ncoghlan


The reason people say that multi-threading is not very efficient in python is because of the Global Interpreter Lock. Because of the way the interpreter is written, only one thread can safely execute code in the interpreter at the same time.

This means that if you have threads which are quite heavily compute bound, that is, doing lots of stuff in the interpreter, then you effectively still only have the performance of a single threaded program. In this case you might be better off using the multiprocessing module, which has the same interface as the multithreading module but launches multiple copies of the interpreter (the downside of this is that you will have to explicitly share memory).

Where you still can reap speed gains from multithreading in python is if you are doing something that is heavily IO bound. While one thread is waiting for disk or network i/o the other threads can still execute, because when threads block they release the interpreter lock.

like image 30
Jesse Cohen Avatar answered Oct 11 '22 22:10

Jesse Cohen