Search code examples
pythonnumpymultiprocessingmpmath

Python: Multiprocessing running portion of code that it shouldn't


I was sort of playing around with multiprocessing and different math libraries to calculate pi and wanted to know how much faster was it with or without multiprocessing by implementing time.perf_counter(). mp.Pool maps 20 threads as the same with my CPU threads count, while processing main, it also processed

etime = time.perf_counter()
print(f"Time Used: {etime - stime:0.4f}sec.\n")

which prints it 20 times.

Result excluded print in solve:

...
...
...

Time Used: 0.0001sec.

Time Used: 0.0001sec.

Time Used: 0.0000sec.

Time Used: 15.0268sec.
import time
import numpy as np
from mpmath import mp
import multiprocessing as mps

stime = time.perf_counter()
mp.dps = 50
accuracy = 6000000
R = 1


def solve(a):
    n = a
    theta = 360 / n
    rad = mp.radians(theta/2)
    print(n*R*mp.sin(rad))


if __name__ == "__main__":
    pool = mps.Pool(mps.cpu_count())
    pool.map(solve, range(1, accuracy))
    pool.close()
    pool.terminate()
    pool.join()


etime = time.perf_counter()
print(f"Time Used: {etime - stime:0.4f}sec.\n")


Solution

  • TLDR: To fix your problem, just indent the last two lines.

    The multiprocessing library actually runs the entire Python script in each thread. As an experiment run python in the same directory as the program and try importing it. You will notice that the bottom two statements will run even though the solve function doesn't

    Therefore, anything that is not in the if __name__ == "__main__": block will be run by each thread. The if __name__ == "__main__": statement tells Python to only run this code in the main thread.

    To solve your problem, you need to add the bottom two lines into the if statement.