[Tutor] how to run function only for a pre-defined time

Alan Gauld alan.gauld at btinternet.com
Sat Oct 27 14:08:26 CEST 2007

"Vinu Vikram" <vvinuv at gmail.com> wrote

> Could anybody try to tell me whether it is possible to run a 
> function only
> for a particular time period. In my python script I call some third 
> party
> function which some time won't give results. I would like to write 
> the
> script in such a way that, it will wait for the result from this 
> function
> for some time, say 10 seconds, and if it fails to get any results 
> within
> that time (10 seconds), the script should skip that function and 
> stop the
> program.

I think you will need to use threading to launch your function in a
separate thread. If the thread dopesn't end in time you can proceed
in the main thread to kill the thread and exit the program.

Read about Python threading here:


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list