Reputation: 83
I noticed that I can access functions and modules in a child process that were outside of the child process function/target. So I'm wondering when I create a child process in python does it copy everything from current process? Why is it I can access functions and imported modules that are outside the child target?
from multiprocessing import Process, Pipe
def test1():
return "hello"
def simpleChildProcess( childPipe ):
# simpleChildProcess can access test1 function
foo = test1()
childPipe.send( foo )
parentPipe, childPipe = Pipe()
childProcess = Process( target=simpleChildProcess, args=(childPipe,) )
childProcess.start()
print "Pipe Contains: %s" % parentPipe.recv()
Upvotes: 7
Views: 1824
Reputation: 879909
On Unix-like OSes, multiprocessing.Process
uses os.fork
to spawn new processes.
fork
creates a new process which is a copy of the parent process, and the forked process resumes from the point where fork
was called.
Since Windows lacks fork
, multiprocessing.Process
starts a new Python process and imports the calling module. On Windows, calls to Process
have to be inside if __name__ == '__main__'
to prevent Process
from being called repeatedly every time the calling module is imported. (It's a good practice even on Unix to include if __name__ == '__main__':
to prevent your code from causing runaway process-spawning).
Thus, the child process has access to functions and modules that have been defined by the calling module up to the point where Process
had been called (in the case of Unix) or after importation of the calling module (in the case of Windows).
Upvotes: 9