473,396 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1947
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Przemysław Różycki | last post by:
Hello, I have written some code, which creates many threads for each connection ('main connection'). The purpose of this code is to balance the load between several connections ('pipes'). The...
3
by: Matt C. | last post by:
This is my first attempt at writing a .NET service, and also my first attempt at using threads. I don't know what I'm doing. Below is some simplified code from my service class (inheriting from...
4
by: John Martin | last post by:
I have a web site that basically builds and displays reports. Some of the reports take a long time to build (up to 10 minutes). I'd like to display a "Please Wait" screen while the report is being...
5
by: Jakub Moskal | last post by:
Hi, I want to write a benchmark that will measure performance of several different algorithms for the same problem. There is a set time bound though, the algorithm cannot run longer than n...
12
by: Perecli Manole | last post by:
I am having some strange thread synchronization problems that require me to better understand the intricacies of Monitor.Wait/Pulse. I have 3 threads. Thread 1 does a Monitor.Wait in a SyncLock...
16
by: Thirsty Traveler | last post by:
I would like to create a test harness that simulates multiple concurrent users executing an individual thread. I would like this to be determined at runtime when the user specifies the number of...
9
by: jdlists | last post by:
I have inheirted some existing code, that i will explain in a moment, have needed to extend and ultimately should be able to run in threads. I've done a bunch of work with python but very little...
1
by: Vishal Sethia | last post by:
Just trying to understand the behaviour of spawn. Consider I have a function which creates two threads. And in one of the threads I make a call to pexpect.spawn. spawn would fork and create a new...
4
by: Falcon | last post by:
Hi all, I want to fill a TreeView with some heave data. this action may take several minutes and I'd like to show to the user a model dialog with a simple ProgressBar ('Marquee' style). I managed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.