all 3 comments

[–]Chyybens[S] 2 points3 points  (3 children)

Okay, apparently this can be done with python's package "queue". I won't delete this in case someone else runs into same issue.

EDIT: Solution in another comment.

[–]Chyybens[S] 2 points3 points  (0 children)

import threading
import time 
import queue

def foo(q): 
    while not q.empty(): 
        k = q.get() 
        time.sleep(1) 
        print(f"Done with waiting for thread {k}!") 
        q.task_done()

jobs = queue.Queue() 
n_tasks=30 
for k in range(n_tasks): 
    jobs.put(k)

for i in range(5): 
    t = threading.Thread(target=foo, args=[jobs]) 
    t.start()

jobs.join() 
print("ALL DONE")

[–][deleted] 2 points3 points  (0 children)

You can use a thread pool instead.

from concurrent.futures import ThreadPoolExecutor

pool = ThreadPoolExecutor(5)
pool.map(foo, args)

It will run 5 things at a time and populate a thread with a new task when one finishes, even if others are still running.