Untitled

mail@pastecode.io avatar
unknown
python
a year ago
674 B
0
Indexable
Never
from time import sleep, monotonic
from random import randint
from multiprocessing import Process, Semaphore

def task(semaphore, number, initial):
    with semaphore:
        pause = randint (1, 3)
        print(f'Process #{number} got pause {pause}')
        print(f'Process #{number}, start {monotonic() - initial}')
        sleep(pause)
        print(f'Process #{number}, end {monotonic() -initial}')
if __name__ == "__main__":
    semaphore = Semaphore (2)
    initial = monotonic ()
    procs = []
    for i in range (6):
        pr = Process (target=task, args=(semaphore, i, initial))
        procs.append(pr)
        pr.start()
    for pr in procs:
        pr.join()