Terminating a function
Ian Leitch
port001 at gentoo.org
Fri Feb 17 13:56:29 EST 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-----
More information about the Python-list
mailing list