Fork Vs Spawn
Fork child inherit all resources from the parent process
Fork child inherit all resources from the parent process
Fork is the default method for multi-processing
Spawn child doesn't inherit any resources from the parent process other than those required to execute the specified callable target.
Spawning a process is slower (because it re-imports) than forking a process
Spawn
Spawn is essentially a combination of fork followed by an exec system call
When a child process is spawned, anything imported at the module level in the (above )__main__ module of the parent process gets reimported in the child
Anything below __main__ will not be copied
test1.py
#########
Inside test1
test1.py
#########
Inside test2
spawn_test.py
############
from multiprocessing import Process
import test1
import test2
def process_task():
print("I am inside child process")
if __name__ == '__main__':
# Change the method to 'spawn' and verify
# that the modules are reimported in the child process
Spawn child doesn't inherit any resources from the parent process other than those required to execute the specified callable target.
Spawning a process is slower (because it re-imports) than forking a process
Spawn
Spawn is essentially a combination of fork followed by an exec system call
When a child process is spawned, anything imported at the module level in the (above )__main__ module of the parent process gets reimported in the child
Anything below __main__ will not be copied
test1.py
#########
Inside test1
test1.py
#########
Inside test2
spawn_test.py
############
from multiprocessing import Process
import test1
import test2
def process_task():
print("I am inside child process")
if __name__ == '__main__':
# Change the method to 'spawn' and verify
# that the modules are reimported in the child process
### Spawn
multiprocessing.set_start_method('spawn')
process = Process(target=process_task)
process.start()
process.join()
print("I am inside parent process")
# Child reimports the test1, test2 module again...
Output:
Inside test1
Inside test2
Inside test1
Inside test2
I am child process
I am parent process
multiprocessing.set_start_method('spawn')
process = Process(target=process_task)
process.start()
process.join()
print("I am inside parent process")
# Child reimports the test1, test2 module again...
Output:
Inside test1
Inside test2
Inside test1
Inside test2
I am child process
I am parent process
No comments:
Post a Comment