Re: two questions about thread

2006-01-16 Thread Bryan Olson
Kevin wrote:
 The best way to do this is by using a flag or event that the child-threads
 monitor each loop (or multiple times per loop if it's a long loop).  If the
 flag is set, they exit themselves.
 
 The parent thread only has to set the flag to cause the children to die.

Doesn't work, because threads can be blocked. Worse,
some threads may be blocked waiting for others to release
them. The unblocked threads check the flag and exit, so
they're never signal the blocked ones.


-- 
--Bryan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: two questions about thread

2006-01-15 Thread Kevin
The best way to do this is by using a flag or event that the child-threads
monitor each loop (or multiple times per loop if it's a long loop).  If the
flag is set, they exit themselves.

The parent thread only has to set the flag to cause the children to die.

Kevin.


Bryan Olson [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 iclinux wrote:
  a.  how to exit the whole process in a thread?
  b. when thread doing a infinite loops, how to terminate the process?:

 As others noted, the threading module offers Thread.setDaemon.
 As the doc says: The entire Python program exits when no active
 non-daemon threads are left.

 Python starts your program with one (non-daemon) thread which
 is sometimes called the main thread. I suggest creating all
 other threads as daemons. The process will then exit when the
 main thread exits.

 If some other thread needs to end the process, it does so by
 telling the main thread to exit. For example, we might leave
 the main thread waiting at a lock (or semaphore), and exit if
 the lock is ever released.


   it seems that the follow doesn't work, in my Windows XP:
   thread.start()
   thread.join()

 Is that part of the questions above, or another issue?


 -- 
 --Bryan


-- 
http://mail.python.org/mailman/listinfo/python-list


two questions about thread

2006-01-14 Thread iclinux
hi there,

I'm new to python, and have two questions:

a.  how to exit the whole process in a thread?
b. when thread doing a infinite loops, how to terminate the process?:
it seems that the follow doesn't work, in my Windows XP:
thread.start()
thread.join()

Regards

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: two questions about thread

2006-01-14 Thread Diez B. Roggisch
iclinux wrote:
 hi there,
 
 I'm new to python, and have two questions:
 
 a.  how to exit the whole process in a thread?

sys.exit()

Works only if Thread.setDaemon(True) is invoked on all threads.

 b. when thread doing a infinite loops, how to terminate the process?:
 it seems that the follow doesn't work, in my Windows XP:
 thread.start()
 thread.join()

It works. You just don't understand _how_ it works. There is no (easy, 
without major pain performance- and stability-wise and corner-case-free) 
way to terminate a thread. If you want that, use a subprocess  e.g. 
pyro as RPC mechanism.

Diez
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: two questions about thread

2006-01-14 Thread Bryan Olson
iclinux wrote:
 a.  how to exit the whole process in a thread?
 b. when thread doing a infinite loops, how to terminate the process?:

As others noted, the threading module offers Thread.setDaemon.
As the doc says: The entire Python program exits when no active 
non-daemon threads are left.

Python starts your program with one (non-daemon) thread which
is sometimes called the main thread. I suggest creating all
other threads as daemons. The process will then exit when the
main thread exits.

If some other thread needs to end the process, it does so by
telling the main thread to exit. For example, we might leave
the main thread waiting at a lock (or semaphore), and exit if
the lock is ever released.


  it seems that the follow doesn't work, in my Windows XP:
  thread.start()
  thread.join()

Is that part of the questions above, or another issue?


-- 
--Bryan
-- 
http://mail.python.org/mailman/listinfo/python-list