470,588 Members | 2,097 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,588 developers. It's quick & easy.

threads

Hi

I have create two threads (from threading module). I want to
synchronize this two in the folowwing way

def Threa1func():
do stuff..
while something
if test:
CHANGE TO Thread2
do stuff...

def Thread2func():
do stuff ...
CHANGE TO Thread1

Thread1 = threading.Thread(target=Thread1func)
Thread2 = threading.Thread(target=Thread2func)

them i've started this two threads

Thread1.start()
Thread2.start()

I want to know if it is posible to do this CHANGE of threads.

thanks in advance

Zunbeltz
--
Remove XXX from email: zu******@wm.lc.ehu.esXXX
Jul 18 '05 #1
3 1942
Zunbeltz Izaola wrote:
Hi

I have create two threads (from threading module). I want to
synchronize this two in the folowwing way


Maybe you don't want threads but coroutines. Google for it, and here's one page
on it: http://www-106.ibm.com/developerwork.../l-pythrd.html
--
--
Every sufficiently advanced magic is indistinguishable from technology
- Arthur C Anticlarke
Jul 18 '05 #2
Zunbeltz Izaola wrote:

I have create two threads (from threading module). I want to
synchronize this two in the folowwing way

def Threa1func():
do stuff..
while something
if test:
CHANGE TO Thread2
do stuff...

def Thread2func():
do stuff ...
CHANGE TO Thread1

Thread1 = threading.Thread(target=Thread1func)
Thread2 = threading.Thread(target=Thread2func)

them i've started this two threads

Thread1.start()
Thread2.start()

I want to know if it is posible to do this CHANGE of threads.


Not exactly, or yes, depending on what you really want to do.

Can you describe the problem in different terms, without reference
to actual Python code or the threading module? It's not at all clear
that you really want to use threads for your problem, and if you
do, you are looking for a different concept than "CHANGE"... maybe
semaphores or something.

-Peter
Jul 18 '05 #3
[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
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Ronan Viernes | last post: by
reply views Thread by Al Tobey | last post: by
34 posts views Thread by Kovan Akrei | last post: by
3 posts views Thread by bygandhi | last post: by
10 posts views Thread by [Yosi] | last post: by
6 posts views Thread by RahimAsif | last post: by
10 posts views Thread by Darian | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.