How can I wait for all the threads I spawn for 5 minutes
Bryan Olson
fakeaddress at nowhere.org
Sun Sep 2 04:18:10 EDT 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
More information about the Python-list
mailing list