Multiprocessing.Process running entire script instead of just target function in python

Viewed 463

I have a simple example script that uses multiprocessing to execute a very simple function and return the runtimes for all parts of the process. The script is fully reproducible and looks as such:

import time
start_time = time.perf_counter()

import multiprocessing
print(f'Libraries loaded: {round(time.perf_counter()-start_time,2)} sec')
start_time = time.perf_counter()


def test():
    print('Sleeping 1 sec')
    time.sleep(1)
    print('Done Sleeping')

print(f'Functions loaded: {round(time.perf_counter()-start_time,2)} sec')
start_time = time.perf_counter()


if __name__ == '__main__':
    p1 = multiprocessing.Process(target=test)
    p2 = multiprocessing.Process(target=test)
    p1.start()
    p2.start()
    p1.join()
    p2.join()

    finish = time.perf_counter()
    print(f'Multiprocessing finished in {round(finish - start_time, 2)} sec')

The output of the script looks as such:

Libraries loaded: 0.03 sec
Functions loaded: 0.0 sec
Libraries loaded: 0.0 sec
Functions loaded: 0.0 sec
Sleeping 1 sec
Libraries loaded: 0.0 sec
Functions loaded: 0.0 sec
Sleeping 1 sec
Done SleepingDone Sleeping

Multiprocessing finished in 1.12 sec

Process finished with exit code 0

As you can see, whilst the multiprocesses are running in parallel, they run the entire script each time as opposed to just executing the target function test. The script is therefore being run twice completely unnecessarily and I don't understand why.

Could someone explain this to me please?

Thanks

1 Answers

Do this:

if __name__ == "__main__":
    <execute main thread here>

inside main thread you create the process. That new process won't call the whole script cause you only allow the main thread to call the whole process.

Related