a very newbie question about threading

C

Chris Patton

Hey Everyone.

I've recenently stumbled upon a concept known as "threading". Can
anyone tell me what this IS and how it is used, or show me where I can
get information about this?

Thanks!
 
C

Chris S.

Chris said:
Hey Everyone.

I've recenently stumbled upon a concept known as "threading". Can
anyone tell me what this IS and how it is used, or show me where I can
get information about this?

Thanks!

Threading, or multi-threading, involves the simultaneous execution of
multiple sequences (e.g. "threads") of instructions. This is similar to
how your computer runs several programs at the same time, except threads
usually share the same space in memory, so they can easily operate on
shared data. As always, Google and Python's docs are your friends:

http://www.google.com/search?hl=en&lr=&q=what+is+multi-threading&btnG=Search
http://docs.python.org/lib/module-threading.html
 
M

Mike Meyer

I've recenently stumbled upon a concept known as "threading". Can
anyone tell me what this IS and how it is used, or show me where I can
get information about this?

Now that you know what they are - be warned that they tend to be a
source of very hard to track down bugs. If you can avoid writing
threaded code, do so. One common use of threads is to handle multiple
IO streams at one time. Asyncore is a much saner solution to that
problem.

<mike
 
P

Peter Hansen

Jeremy said:
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...

-Peter
 
J

Jeremy Bowers

Hey Everyone.

I've recenently stumbled upon a concept known as "threading". Can
anyone tell me what this IS and how it is used, or show me where I can
get information about this?

Chris S. gave you the bare-bones basic.

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", that randomly appears and disappears, resisting capture and
elimination by even the best of 'em. It's a complicated topic that gives
even the most expert programmers pause. I'm not exaggerating in the
slightest.

Generally, unless you *know* you need them, you don't, and there are ways
around using threads. Threads are kind of like the old "goto" statements;
it isn't that the idea of "goto" is intrinsically bad, it is just that
without discipline they rapidly become more problem than solution. So
instead of "goto", we use certain special cases of them, like function
calls and loops, that behave much more nicely. Most thread uses can
equally well be satisfied with certain other "special cases" that are much
less dangerous, like the "async" module for processing multiple incoming
files(/sockets/pipes whatever), or firing off seperate processes for long
running computations.

That said, I have used them when they were the best tool for the job. But
raw threads scare me every time.
 
J

Jeremy Bowers

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...

I've been looking over the recently announced "Candygram" module, too, and
while I must admit I haven't used it it does look **very** nice and I
think I will next time it makes sense.

http://candygram.sourceforge.net/

Took me a little bit to figure out exactly how to ask a foreign thread for
a value synchronously but the sample program "program_5.2.py" shows how
(and the _alt program does it in Python better). This makes more sense if
you read along in the Erlang book as suggested.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,211
Messages
2,571,092
Members
47,693
Latest member
david4523

Latest Threads

Top