Executing python script stored as a string

Hendrik van Rooyen hendrik at microcorp.co.za
Tue Sep 1 13:33:53 CEST 2009

On Tuesday 01 September 2009 11:32:29 Steven D'Aprano wrote:

> Possibly there is a way to have a thread halt itself after a certain
> amount of time? I'm not an expert on threads, I've hardly ever used them.

Not automagically, as far as I can see.
You are on your own if you want to somehow kill a thread.

What I do is to wrap what I want to do in a while clause:

while runbool:

where runbool is a global that I set to false elsewhere when I want to stop.

There is of course nothing to stop you writing a thread with something like:

import time
start_time = time.time()
while time.time() - start_time < some_parameter:

Which will have the effect of running for a while and then stop.
I cannot see much use use for that as it is, as it will be bloody-minded about 
the time, and might chop off before the whole job is done, but one could make 
it more intelligent, such that it keeps track of idle time, and aborts after 
say a second (or a minute) of not doing anything useful.

Unlike you, I am a thread and queue fanatic - use them all the time.

- Hendrik

More information about the Python-list mailing list