threads

Alan Kennedy alanmk at hotmail.com
Tue Nov 11 12:08:48 EST 2003


[Zunbeltz Izaola]
> I want to know if it is posible to do this CHANGE of threads.

It is not possible to "switch" execution between threads like this:
the whole point of threads is that there multiple execution contexts
operating in parallel.

Once a thread has been started, it must run all the way through until
it's execution terminates naturally. You cannot even raise exceptions
in one thread from another thread.

What you *can* do is communicate information between threads. This is
most often done by using a Queue object, which you can read about here

http://www.python.org/doc/current/lib/module-Queue.html

So a good design pattern for what (I think) you're trying to do is to
devise a class/object which represents the work to be done, and send
that "work object" back and forth on the Queue between the threads.
This way, it "appears" that the work is actually moving between
threads. When there is no work for a thread to do, it is blocked in a
Queue.get call, waiting for something to do. But it does not "go
away".

Of course, this is just one way to do it: another poster suggested
another excellent approach: co-routines. 

HTH,

-- 
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan:              http://xhaus.com/mailto/alan




More information about the Python-list mailing list