[Tutor] computers

Jeff Shannon jeff@ccvcorp.com
Thu, 06 Jun 2002 09:47:32 -0700


"Scot W. Stevenson" wrote:

> Hi -
>
> > It might be fun to
> > practice some multi threaded Python programming with to procs.
>
> Wait a moment. I thought that Python threads weren't really run in parallel
> because of something called the Great Interpreter Lock. Wouldn't this mean
> that you can have all the processors you want, it will not make Python
> faster?

Actually, it's the Global (not great) Interpreter Lock, but this is largely
true.  The Python interpreter itself has been made "threadsafe" in a rather
crude (but effective) way -- by only allowing a single Python thread to run at
a time.  This means that the only possible way that multithreading can speed up
a Python program is if it spends a fair amount of time either waiting for I/O,
or in C extensions (which release the GIL).  Of course, considering how few
people use multiproc systems, and that few of *those* will distribute threads
from a single process across different processors, the GIL is much less of a
bottleneck than it sounds like.  Especially when you consider that the majority
of real-world programs are I/O bound (or memory bound), not CPU bound -- the
processor spends a fair amount of time sitting idle anyhow, so squeezing CPU
efficiency any further tends to not have much practical effect.

The real advantage to multithreading is that it separates your program into
distinct, logical tasks, making it easier to understand what's going on (if
you're separating tasks right ;) ).  Only in very special cases does
multithreading really help with program speed.


> (And, concerning another list thread here, wouldn't this be another reason
> to use Java instead of Python?)

In a word, no.  ;)  First of all, on many platforms Java (IIRC) emulates
threads in a nonthreaded environment ("green" threads), with semantics that are
similar to Python's GIL (but weaker, because the GIL is typically released in
extensions).  Second, and more importantly, efficient use of multiple
processors (even if Java offered that) is typically a poor reason to choose a
language to develop in -- as mentioned above, it's probably not going to result
in a faster program.  And if you really needed that efficiency, then you should
be coding in C, not Java.

The point of using Python is that it's easier/faster to develop software with
it.  The (hypothetical) advantages that Java offers for SMP environments are
rarely going to be enough of a benefit to override the extra costs of
developing in Java.

Jeff Shannon
Technician/Programmer
Credit International