Good day ! I can not understand the threading module. I break my head all day. Run 4 parallel threads. The operation time of each stream is 0.2 to 1 second. Threads run in an infinite loop. It is important that each thread wait for the completion of the most recent thread and only then begin a new cycle.

import threading import time def write(x,delay): while True: time.sleep(delay) print("Process # {} is finished ...".format(x)) t1 = threading.Thread(target=write, args=[1,0.2]) t2 = threading.Thread(target=write1, args=[2,0.4]) t4 = threading.Thread(target=write1, args=[3,0.6]) t3 = threading.Thread(target=write1, args=[4,1]) t1.start() t2.start() t3.start() t4.start() 

    1 answer 1

    For such a problem is ideal barrier :

     import threading import time b = threading.Barrier(4, timeout=5) def write(x, delay): while True: time.sleep(delay) print("Process # {} is finished ...".format(x)) b.wait() t1 = threading.Thread(target=write, args=[1, 0.2]) t2 = threading.Thread(target=write, args=[2, 0.4]) t4 = threading.Thread(target=write, args=[3, 0.6]) t3 = threading.Thread(target=write, args=[4, 1.0]) t1.start() t2.start() t3.start() t4.start()