Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How can I wait for all the threads I spawn for 5 minutes

Reply
Thread Tools

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

 
 
herman
Guest
Posts: n/a
 
      08-31-2007
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.

 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      09-01-2007
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 -------------

 
Reply With Quote
 
 
 
 
Bryan Olson
Guest
Posts: n/a
 
      09-02-2007
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
 
Reply With Quote
 
Antoon Pardon
Guest
Posts: n/a
 
      09-03-2007
On 2007-08-31, herman <(E-Mail Removed)> 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....33130893cee567
 
Reply With Quote
 
Fabio Z Tessitore
Guest
Posts: n/a
 
      09-03-2007
> [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
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Logging stdout/stderr/stdin of an spawn process (Open4::spawn) Edgardo Hames Ruby 1 05-06-2008 08:17 PM
Logging stdout/stderr/stdin of an spawn process (Open4::spawn) Ed Hames Ruby 0 04-16-2008 04:21 PM
How can I tell when the threads that I spawn are done in python ogerchikov@gmail.com Python 1 03-24-2007 05:16 AM
spawn syntax + os.P_WAIT mode behavior + spawn stdout redirection Derek Basch Python 2 01-21-2005 05:37 AM



Advertisments