I have a program in which I would like to run one of the functions in parallel for several arguments.
the program is in the following format:
import statements
def function1():
do something
def function2()
do something
def main():
function1()
I found several examples of how to use the multiprocessing
library online such as the following general template
import multiprocessing
def worker(num):
print 'Worker:', num
return
if __name__ == '__main__':
for i in range(5):
p = multiprocessing.Process(target=worker, args=(i,))
p.start()
From what I understand the worker()
is the function which is meant to be performed in parallel. But I am not sure where or how to use the
(if __name__ == '__main__':
) block the code.
as of now that block is in my main()
and when I run the program I do not get the worker function executed multiple times, instead my main gets executed multiple times?
So where is the proper place to put the (if __name__ == '__main__':
) block