a very newbie question about threading

Jp Calderone exarkun at divmod.com
Sat Nov 13 00:15:29 EST 2004


On Fri, 12 Nov 2004 23:01:37 -0500, Peter Hansen <peter at engcorp.com> wrote:
>Jeremy Bowers wrote:
> > On Fri, 12 Nov 2004 16:29:53 -0800, Chris Patton wrote:
> >>I've recenently stumbled upon a concept known as "threading". 
> > 
> > I'd add that with no insult intended, "threading" and "very newbie" do not
> > go together well. Threading is the most common source of the notorious
> > "heisenbug", ...
> > Generally, unless you *know* you need them, you don't, ...
> 
> Excellent post, Jeremy, though I think it's worth adding something
> like this:
> 
> Python makes writing threaded code much more straightforward than
> many languages, and also provides some protection from some types
> of bugs which are almost ubiquitous in threaded applications in
> some other languages.  By sticking with the Queue object (see the
> module by that name) as the sole means of communication between
> threads, the vast majority of other thread-related problems
> simply disappears.  Not all of them -- and Jeremy's caution about
> threads giving even expert programmers pause applies -- but in
> general Python's not a bad place to learn and use threads...

  As long as we're pointing out aspects of threading particular to Python, then we should include this:

  The relative ease of use associated with Python threads do not come without a cost.  A prime use of threads is to take advantage of multiple processors, truly running multiple instruction streams simultaneously (instead of only semi-randomly interlacing them as will occur on a single processor machine).  A Python thread, however, acquires an exclusive "lock" when running which prevents any other Python thread in the same process from running.  This means that only one Python thread is ever actually running at a time, negating any multiprocessor benefits threading might have yielded.  So while threads in Python are less painful than threads elsewhere, they are also less advantageous.

  Jp



More information about the Python-list mailing list