Why is the section that should only run once running every time? Does this make sense and what is the proper way to run a target function as a separate thread ?
The code is taken from a very simple example at https://pymotw.com/2/multiprocessing/basics.html
import multiprocessing
print("Starting Main - This should only run ONCE")
def worker1(num):
"""thread worker function"""
print("Worker:", num)
return
if __name__ == '__main__':
jobs = []
for i in range(5):
p = multiprocessing.Process(target=worker1, args=(i,))
jobs.append(p)
print("Starting Process=", i)
p.start()
OUTPUT Console:
[Running] python "e:\Projects\Python-Test\test1.py"
Starting Main - This should only run ONCE
Starting Process= 0
Starting Process= 1
Starting Process= 2
Starting Process= 3
Starting Process= 4
Starting Main - This should only run ONCE # (??????? WHY )
Worker: 1
Starting Main - This should only run ONCE (???????)
Worker: 0
Starting Main - This should only run ONCE
Worker: 4
Starting Main - This should only run ONCE
Worker: 2
Starting Main - This should only run ONCE
Worker: 3
[Done] exited with code=0 in 0.45 seconds