How to force a thread to stop

bryanjugglercryptographer at yahoo.com bryanjugglercryptographer at yahoo.com
Sat Jul 22 11:42:56 EDT 2006


Hans wrote:
> Hi all,
>
> Is there a way that the program that created and started a thread also stops
> it.
> (My usage is a time-out).
>
> E.g.
>
> thread = threading.Thread(target=Loop.testLoop)
> thread.start()     # This thread is expected to finish within a second
> thread.join(2)    # Or time.sleep(2) ?

No, Python has no threadicide method, and its absence is not an
oversight. Threads often have important business left to do, such
as releasing locks on shared data; killing them at arbitrary times
tends to leave the system in an inconsistent state.

> if thread.isAlive():
>     # thread has probably encountered a problem and hangs
>     # What should be here to stop thread  ??????

At this point, it's too late. Try to code so your threads don't hang.

Python does let you arrange for threads to die when you want to
terminate the program, with threading's Thread.setDaemon().


-- 
--Bryan




More information about the Python-list mailing list