views:

1365

answers:

6

A reliable coder friend told me that Python's current multi-threading implementation is seriously buggy - enough to avoid using altogether. What can said about this rumor?

A: 

I've used it in several applications and have never had nor heard of threading being anything other than 100% reliable, as long as you know its limits. You can't spawn 1000 threads at the same time and expect your program to run properly on Windows, however you can easily write a worker pool and just feed it 1000 operations, and keep everything nice and under control.

tj9991
+3  A: 

As far as I know there are no real bugs, but the performance when threading in cPython is really bad (compared to most other threading implementations, but usually good enough if all most of the threads do is block) due to the GIL (Global Interpreter Lock), so really it is implementation specific rather than language specific. Jython, for example, does not suffer from this due to using the Java thread model.

See this post on why it is not really feasible to remove the GIL from the cPython implementation, and this for some practical elaboration and workarounds.

Do a quick google for "Python GIL" for more information.

Henrik Gustafsson
I don't know if I would say that python's threading is inefficient. It's just that you can't take advantage of multicore machines.
Jason Baker
I would, and this talk agrees with me, and provides ample examples: http://blip.tv/file/2232410 In most cases it won't matter though. Still, see the talk, it's great!
Henrik Gustafsson
+29  A: 

Python threads are good for concurrent I/O programming. Threads are swapped out of the CPU as soon as they block waiting for input from file, network, etc. This allows other Python threads to use the CPU while others wait. This would allow you to write a multi-threaded web server or web crawler, for example.

However, Python threads are serialized by the GIL when they enter interpreter core. This means that if two threads are crunching numbers, only one can run at any given moment. It also means that you can't take advantage of multi-core or multi-processor architectures.

There are solutions like running multiple Python interpreters concurrently, using a C based threading library. This is not for the faint of heart and the benefits might not be worth the trouble. Let's hope for an all Python solution in a future release.

Avner
The solution nowadays is to use the multiprocessing module.
nosklo
Python threads are certainly REAL CPU threads.
zweiterlinde
I don't know what "CPU threads" means, but Python threads *are* OS threads.
ΤΖΩΤΖΙΟΥ
+4  A: 

The GIL (Global Interpreter Lock) might be a problem, but the API is quite OK. Try out the excellent processing module, which implements the Threading API for separate processes. I am using that right now (albeit on OS X, have yet to do some testing on Windows) and am really impressed. The Queue class is really saving my bacon in terms of managing complexity!

EDIT: it seaes the processing module is being included in the standard library as of version 2.6 (import multiprocessing). Joy!

Daren Thomas
+10  A: 

The standard implementation of Python (generally known as CPython as it is written in C) uses OS threads, but since there is the Global Interpreter Lock, only one thread at a time is allowed to run Python code. But within those limitations, the threading libraries are robust and widely used.

If you want to be able to use multiple CPU cores, there are a few options. One is to use multiple python interpreters concurrently, as mentioned by others. Another option is to use a different implementation of Python that does not use a GIL. The two main options are Jython and IronPython.

Jython is written in Java, and is now fairly mature, though some incompatibilities remain. For example, the web framework Django does not run perfectly yet, but is getting closer all the time. Jython is great for thread safety, comes out better in benchmarks and has a cheeky message for those wanting the GIL.

IronPython uses the .NET framework and is written in C#. Compatibility is reaching the stage where Django can run on IronPython (at least as a demo) and there are guides to using threads in IronPython.

Hamish Downer
CPython does not use Green Threads. It uses native OS threads, but the Global Interpreter Lock means that they behave in a similar way to green threads (as only one thread can run at a time).
John Montgomery
Seconded. CPython does not use green threads.
Alec Thomas
+1  A: 

If you want to code in python and get great threading support, you might want to check out IronPython or Jython. Since the python code in IronPython and Jython run on the .NET CLR and Java VM respectively, they enjoy the great threading support built into those libraries. In addition to that, IronPython doesn't have the GIL, an issue that prevents CPython threads from taking full advantage of multi-core architectures.

m-sharp