Multitasking example with wait



examples/multitasking/example_with_wait.py
import multitasking
import time
import random

multitasking.set_max_threads(2)

@multitasking.task
def work(ix, sec):
    print(f"Start {ix} sleeping for {sec}s")
    time.sleep(sec)
    print(f"Finish {ix}")

if __name__ == "__main__":
    tasks = (6, 0.7, 0.8, 0.3, 0.4, 3, 0.1)
    for ix, sec in enumerate(tasks):
        work(ix+1, sec)
    multitasking.wait_for_tasks()

    print("do some work after all the jobs are done")

Start 1 sleeping for 6s
Start 2 sleeping for 0.7s
Finish 2
Start 3 sleeping for 0.8s
Finish 3
Start 4 sleeping for 0.3s
Finish 4
Start 5 sleeping for 0.4s
Finish 5
Start 6 sleeping for 3s
Finish 6
Start 7 sleeping for 0.1s
Finish 7
Finish 1
do some work after all the jobs are done