It is common in python multiprocessing to use if __name__ == "__main__"
. However, if I know my child process does not need anything from __main__
module, can I remove this part? e.g.
# test_child.py
from multiprocessing import Process, get_context
def f(x):
print(f"{x}")
def start():
ctx = get_context("spawn")
p = ctx.Process(target=f, args=("hello",))
p.start()
p.join()
# test_parent.py
from test_child import start
start()
When I run python test_parent.py
, ideally the child process does not need anything from the __main__
module, so it can skil this part, and I don’t need to add if __name__ == "__main__"
in test_parent.py
.
Currently it will cause error though.