Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: two questions about thread

1 view
Skip to first unread message
Message has been deleted

Diez B. Roggisch

unread,
Jan 14, 2006, 12:12:53 PM1/14/06
to
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

Message has been deleted

Bryan Olson

unread,
Jan 14, 2006, 5:37:00 PM1/14/06
to
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

Kevin

unread,
Jan 15, 2006, 9:26:04 AM1/15/06
to
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" <fakea...@nowhere.org> wrote in message
news:g2fyf.888$H71...@newssvr13.news.prodigy.com...

Bryan Olson

unread,
Jan 16, 2006, 4:39:43 PM1/16/06
to
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

0 new messages