Terminating a function

Discussion in 'Python' started by Astan Chee, Feb 17, 2006.

  1. Astan Chee

    Astan Chee Guest

    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
    Astan Chee, Feb 17, 2006
    #1
    1. Advertising

  2. 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
    Diez B. Roggisch, Feb 17, 2006
    #2
    1. Advertising

  3. Astan Chee

    Guest

    , Feb 17, 2006
    #3
  4. Astan Chee

    Ian Leitch Guest

    -----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-----
    Ian Leitch, Feb 17, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman

    Terminating a thread from the main thread

    Charles A. Lackman, Dec 9, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,540
    Herfried K. Wagner [MVP]
    Dec 9, 2004
  2. Kristofer Liljeblad

    Terminating a authenticated user

    Kristofer Liljeblad, Jun 30, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    465
    Kristofer Liljeblad
    Jun 30, 2003
  3. Hiren Patel
    Replies:
    2
    Views:
    449
    Hiren Patel
    Feb 1, 2005
  4. darrel

    terminating multiple try/catches

    darrel, Apr 4, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    355
    Eric Newton
    Apr 6, 2005
  5. lasek
    Replies:
    9
    Views:
    2,457
    Rufus V. Smith
    May 20, 2005
Loading...

Share This Page