I wrote this code to learn multiprocessing in Python.
import concurrent.futures
print("A")
tic = time.perf_counter()
def test():
k = 1
s = 0
print(f"Starting ... ")
for i in range(10000000):
if i % 2 == 0:
s += 4/k
else:
s -= 4/k
k += 2
print(f"... finished!")
def main():
with concurrent.futures.ProcessPoolExecutor() as executor:
executor.submit(test)
executor.submit(test)
if __name__ == '__main__':
main()
toc = time.perf_counter()
print(f"Took {toc - tic:0.4f} seconds")
I would expect a plain output of the seconds but it instead runs print("A") three times too:
A
A
Took 0.0000 seconds
Starting ...
A
Took 0.0000 seconds
Starting ...
... finished!
... finished!
Took 4.0650 seconds
I looked up various examples across google but can't seem to nail down the problem. Any suggestions?