Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python multi-thread join with timeout

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?

like image 984
Tiancheng Liu Avatar asked Sep 03 '26 01:09

Tiancheng Liu


1 Answers

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.

like image 141
pilcrow Avatar answered Sep 05 '26 15:09

pilcrow