471,616 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,616 software developers and data experts.

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

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.

Aug 31 '07 #1
4 1876
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 -------------

Sep 1 '07 #2
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
Sep 2 '07 #3
On 2007-08-31, herman <He************@gmail.comwrote:
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
Sep 3 '07 #4
[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
Sep 3 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Przemysław Różycki | last post: by
4 posts views Thread by John Martin | last post: by
5 posts views Thread by Jakub Moskal | last post: by
12 posts views Thread by Perecli Manole | last post: by
16 posts views Thread by Thirsty Traveler | last post: by
9 posts views Thread by jdlists | last post: by
1 post views Thread by Vishal Sethia | last post: by
4 posts views Thread by Falcon | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.