It seems that if you have a loop of n threads and join them one by one with the timeout t, the actual time you take is n * t because the beginning to count timeout of one child thread is the ending time of last child thread. Is there any way to reduce this total time to t not n*t?
解决方案
Yes, you can calculate an absolute timeout, and recompute your remaining relative timeout before every join:
# Join the_threads, waiting no more than some_relative_timeout to
# attempt to join all of them.
absolute_timeout = time.time() + some_relative_timeout
for thr in the_threads:
timeout = absolute_timeout - time.time()
if timeout < 0:
break
thr.join(timeout)
if thr.isAlive():
# we timed out
else:
# we didn't
Now, whether or not you *should* do this is a bit opaque. It may be better to have "daemon" worker threads communicate their completion by other means: a global state table, pushes of "done" messages to a queue, etc.