Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Terminating a function

Reply
Thread Tools

Terminating a function

 
 
Astan Chee
Guest
Posts: n/a
 
      02-17-2006
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?
Thanks
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-17-2006
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
 
Reply With Quote
 
 
 
 
wittempj@hotmail.com
Guest
Posts: n/a
 
      02-17-2006
This might be all you need:
http://aspn.activestate.com/ASPN/Coo.../Recipe/473878

 
Reply With Quote
 
Ian Leitch
Guest
Posts: n/a
 
      02-17-2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Astan Chee wrote:
> 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!


You can use a thread to do the timing and send an interrupt when the
time is up:

#!/usr/bin/python

import thread
import threading
from time import sleep

def timer_thread():

sleep(10)
thread.interrupt_main()

def my_func():

try:

while 1:

sleep(2)
print "Hello"

except KeyboardInterrupt:

return

my_thread = threading.Thread(target=timer_thread)
my_thread.start()

my_func()

print "Done."
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFD9hxcefZ4eWAXRGIRAnwIAKCH/pH7dyzjk1rsfvFKzrA48GgeEACfdNx6
QmsqAruOESUe2bzPTh/nsE8=
=LvT6
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
strlen function + include the terminating null character ? lasek C Programming 9 05-20-2005 02:05 PM
Terminating a thread from the main thread Charles A. Lackman ASP .Net 3 12-09-2004 02:12 PM
Cisco 827 Config. for terminating an IPSec VPN with xauth.... Masud Reza Cisco 0 01-29-2004 11:36 AM
question about terminating adsl sessions on 3640 Chad Whitten Cisco 0 10-30-2003 02:38 PM
Terminating GRE tunnel on HSRP address Fred Leckie Cisco 0 10-28-2003 08:46 AM



Advertisments