This reminds us once again of the importance of instantiating the Process object within the main section: this is because the child process created imports the script file where the target function is contained. Then, by instantiating the process object within this block, we prevent an infinite recursive call of such instantiations.
A valid workaround is used to define the target function in a different script, namely myFunc.py:
def myFunc(i):
print ('calling myFunc from process n°: %s' %i)
for j in range (0,i):
print('output from myFunc is :%s' %j)
return
The main program containing the process instance is defined in a second file (spawning_processes_namespace.py):
import multiprocessing
from myFunc import myFunc
if __name__ == '__main__':
for i in range(6):
process = multiprocessing.Process(target=myFunc, args=(i,))
process.start()
process.join()
To run this example, type the following command:
> python spawning_processes_names.py
The output is the same as the previous example.