Terminating a function

Astan Chee stanc at al.com.au
Fri Feb 17 07:12:11 EST 2006


I was thinking of threads but I just simply want to terminate a (global) 
function after it has been running for more than 5 minutes regardless of 
state.
I was assuming I needed threads because it can calculate time elapsed 
(that and im rather inexperienced with threads)
Thanks again for your help!

Diez B. Roggisch wrote:

>Astan Chee wrote:
>
>  
>
>>Hi,
>>Im rather new to threads in python but Im trying to terminate a function
>>call (or the execution of the function) after a certain period of time
>>has passed.
>>Do i need to use threads with this? if i do, how do i go about doing it?
>>    
>>
>
>Yes and no. As you didn't tell us muc about your actual problem, threads
>might be what you need. But you _can't_ just stop a thread - however,
>depending on what you do, you can for example regularly check for a flag
>inside your function and then  terminate:
>
>class Foo(threading.Thread):
>   def __init__(self):
>      super(Foo, self).__init__()
>      self.setDaemon(True)
>      self.interrupted = False
>
>   def run(self):
>       while not self.interrupted:
>           do_some_work_but_make_sure_to_come_back_regularly()
>
>
>f = Foo()
>f.start()
>
>time.sleep(10)
>f.interrupted = True
>
>Diez
>  
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060217/a4f6dc89/attachment.html>


More information about the Python-list mailing list