How can I wait for all the threads I spawn for 5 minutes

Discussion in 'Python' started by herman, Sep 1, 2007.

  1. herman

    herman Guest

    Hi,

    In my python program, I would to like to spwan 5 threads, for the them
    for 5 minutes maximum and the continue. Here is my script:

    threads = []

    for j in range(5):
    t = MyThread()
    threads.append(t)

    for t in threads:
    t.join(60*5)
    print "thread join\n"

    # wait for 5 minutes for all the threads to complete ,
    and
    # then continue

    But this code ends up waiting 5 minutes for **each** thread. that is
    not what I want. I just want to wait for 5 minutes for all threads.
    how can I do that?

    And after 5 minutes, i want to kill off all the threads I spawn
    earlier, how can I do that in python.

    Thank you for any help.
    herman, Sep 1, 2007
    #1
    1. Advertising

  2. herman

    Steve Holden Guest

    herman wrote:
    > Hi,
    >
    > In my python program, I would to like to spwan 5 threads, for the them
    > for 5 minutes maximum and the continue. Here is my script:
    >
    > threads = []
    >
    > for j in range(5):
    > t = MyThread()
    > threads.append(t)
    >
    > for t in threads:
    > t.join(60*5)
    > print "thread join\n"
    >
    > # wait for 5 minutes for all the threads to complete ,
    > and
    > # then continue
    >
    > But this code ends up waiting 5 minutes for **each** thread. that is
    > not what I want. I just want to wait for 5 minutes for all threads.
    > how can I do that?
    >
    > And after 5 minutes, i want to kill off all the threads I spawn
    > earlier, how can I do that in python.
    >
    > Thank you for any help.
    >

    Well, to answer your second question, there is no reliable way to kill a
    thread without having the thread periodically examine some aspect of
    its environment that the main thread can change to indicate the
    requirement that the sub-thread terminate.

    The easiest way to check the number of outstanding threads is to use
    threading.activeCount(), which tells you how many outstanding threads
    remain.

    Here's some code I used in a test program recently:

    # As long as we have more than just the 'main' thread running,
    # print out a status message
    while threading.activeCount() > 1 :
    print "-- after", iterCount, "sleeps", \
    str(threading.activeCount()), "threads running in total"
    iterCount += 1
    time.sleep(1)
    print "Only main thread remains"

    Clearly you will need to set some termination flag (a global variable
    would do) after time is up, and as long as your threads examine this
    flag you'll be good to go.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    --------------- Asciimercial ------------------
    Get on the web: Blog, lens and tag the Internet
    Many services currently offer free registration
    ----------- Thank You for Reading -------------
    Steve Holden, Sep 1, 2007
    #2
    1. Advertising

  3. herman

    Bryan Olson Guest

    herman wrote:

    > In my python program, I would to like to spwan 5 threads, for the them
    > for 5 minutes maximum and the continue. Here is my script:
    >
    > threads = []
    >
    > for j in range(5):
    > t = MyThread()
    > threads.append(t)
    >
    > for t in threads:
    > t.join(60*5)
    > print "thread join\n"
    >
    > # wait for 5 minutes for all the threads to complete , and
    > # then continue
    >
    > But this code ends up waiting 5 minutes for **each** thread. that is
    > not what I want. I just want to wait for 5 minutes for all threads.
    > how can I do that?


    Untested:

    from time import time

    deadline = time() + 60*5
    for t in threads:
    t.join(max(0, deadline - time()))


    > And after 5 minutes, i want to kill off all the threads I spawn
    > earlier, how can I do that in python.


    That's harder. Python has no threadicide method, and its
    absence is not an oversight. Can you arrange for your threads
    to check a flag periodically, or might they be hung? Your
    other choice is to end the entire process.


    --
    --Bryan
    Bryan Olson, Sep 2, 2007
    #3
  4. On 2007-08-31, herman <> wrote:
    > Hi,
    >
    > In my python program, I would to like to spwan 5 threads, for the them
    > for 5 minutes maximum and the continue. Here is my script:
    >
    > threads = []
    >
    > for j in range(5):
    > t = MyThread()
    > threads.append(t)
    >
    > for t in threads:
    > t.join(60*5)
    > print "thread join\n"
    >
    > # wait for 5 minutes for all the threads to complete ,
    > and
    > # then continue
    >
    > But this code ends up waiting 5 minutes for **each** thread. that is
    > not what I want. I just want to wait for 5 minutes for all threads.
    > how can I do that?
    >
    > And after 5 minutes, i want to kill off all the threads I spawn
    > earlier, how can I do that in python.


    You may wish to look at this. No guarantee though.

    http://groups.google.com/group/comp...ee567?lnk=st&q=&rnum=2&hl=nl#5833130893cee567
    Antoon Pardon, Sep 3, 2007
    #4
  5. > [cut]
    > But this code ends up waiting 5 minutes for **each** thread. that is
    > not what I want. I just want to wait for 5 minutes for all threads. how
    > can I do that?


    I've written a little code for you using threading. Hope it will help you:

    #! /usr/bin/env python
    # -*- coding: utf8 -*-

    import threading
    import time

    class MyLittleThread(threading.Thread):

    def __init__(self):
    threading.Thread.__init__(self)

    def run(self):
    time.sleep(5)


    if __name__ == "__main__":
    # create a list of 10 MyLittleThread
    # total time to wait is 5 sec (NOT 5sec*10)
    lt = []
    for i in range(10):
    lt.append(MyLittleThread())

    for th in lt:
    th.start()

    bye
    Fabio Z Tessitore, Sep 3, 2007
    #5
    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. Jeff Rodriguez
    Replies:
    23
    Views:
    1,112
    David Schwartz
    Dec 9, 2003
  2. Derek Basch
    Replies:
    2
    Views:
    1,301
    Donn Cave
    Jan 21, 2005
  3. Replies:
    1
    Views:
    203
    Kushal Kumaran
    Mar 24, 2007
  4. Ed Hames
    Replies:
    0
    Views:
    372
    Ed Hames
    Apr 16, 2008
  5. Edgardo Hames
    Replies:
    1
    Views:
    346
    Ed Hames
    May 6, 2008
Loading...

Share This Page