Home > database >  Why my program with semaphore prints 20 secs instead of 10 secs?
Why my program with semaphore prints 20 secs instead of 10 secs?

Time:11-06

Following is my simplified program from my main project. I'm using Semaphore to only allow exactly two processes execute test function at a time. If I'm not out of my mind, the program should only have 10 secs running time but instead I had 20 secs. How do I fix it to reduce my program running time down to 10 secs?

Note: tested with Sublime on Windows 10.

import time
from multiprocessing import Semaphore, Lock, Process

def test(sem):
    sem.acquire()
    time.sleep(5)
    sem.release()

if __name__ == '__main__':
    sem = Semaphore(2)
    processes = []
    
    for _ in range(4):
        processes.append(Process(target=test, args=(sem,)))

    start = time.perf_counter()
    
    for process in processes:
        process.start()
        process.join()

    end = time.perf_counter() - start

    print(f'program finished in {end} secs')

Output

program finished in 20.836512662 secs
[Finished in 21.1s]

CodePudding user response:

for process in processes:
        process.start()
        process.join()

You are starting each process, then immediately waiting for it to finish.

That's 4 processes each doing a 5 sec wait. Hence the 20 secs. There's no actual parallelism in your code.

What you want is to start off the processes all at the same time. Then wait for each to finish:

for process in processes:
        process.start()  # start each
for process in processes:
        process.join()   # wait for all to finish

Which results in:

program finished in 10.129543458 secs
  • Related