two questions about thread

I

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
 
D

Diez B. Roggisch

iclinux said:
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
 
D

Dennis Lee Bieber

a. how to exit the whole process in a thread?

There is no clean way to kill a thread from the outside. Daemon
threads, supposedly, will die when the main program exits. Otherwise,
the main program has to hoist up a flag, and the thread(s) (presumably
they have some sort of loop) have to commit suicide when they see the
flag.
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()

.join says "WAIT for the specified thread to exit", probably working
exactly as it is supposed to.
--
 
B

Bryan Olson

iclinux said:
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?
 
K

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

Bryan Olson

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

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

No members online now.

Forum statistics

Threads
474,276
Messages
2,571,384
Members
48,073
Latest member
ImogenePal

Latest Threads

Top