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

H

herman

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.
 
S

Steve Holden

herman said:
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 -------------
 
B

Bryan Olson

herman said:
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.
 
A

Antoon Pardon

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
 
F

Fabio Z Tessitore

[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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top