Python - Join Multiple Threads With Timeout

multiprocessing, python

Solution

You could loop over each thread repeatedly, doing non-blocking checks to see if the thread is done:

import time

def timed_join_all(threads, timeout):
    start = cur_time = time.time()
    while cur_time <= (start + timeout):
        for thread in threads:
            if not thread.is_alive():
                thread.join()
        time.sleep(1)
        cur_time = time.time()

if __name__ == '__main__':
    for thread in threads:
        thread.start()

    timed_join_all(threads, 60)

Problem

I have multiple Process threads running and I'd like to join all of them together with a timeout parameter. I understand that if no timeout were necessary, I'd be able to write: ``` for thread in threads: thread.join() ``` One solution I thought of was to use a master thread that joined all the threads together and attempt to join that thread. However, I received the following error in Python: ``` AssertionError: can only join a child process ``` The code I have is below. ``` def join_all(threads): for thread in threads: thread.join() if __name__ == '__main__': for thread in threads: thread.start() master = multiprocessing.Process(target=join_all, args=(threads,)) master.start() master.join(timeout=60) ```

Original source

Related problems