I have two files. One is to create and return a process. Another is to create many multiple processes asynchronously.
The problem I have is, why line print("all process created")
(line 13) in the second file has executed 3 times?
import time
import multiprocessing
def wait(s):
print(f"Waiting for {s} seconds...")
time.sleep(s)
print(f"Done Waiting for {s} seconds...")
def create_process(sec):
p1 = multiprocessing.Process(target=wait, args=(sec, ))
p1.start()
return p1
from process1 import create_process
import time
procs = []
def many_process():
global procs
if __name__ == "__main__":
for i in range(1,4):
print(f"creating process to sleep {i}")
p = create_process(i)
procs += [p]
print("all process created")
many_process()
for p in procs:
p.join()
creating process to sleep 1
creating process to sleep 2
creating process to sleep 3
all process created
all process created
Waiting for 1 seconds...
all process created
Waiting for 3 seconds...
all process created
Waiting for 2 seconds...
Done Waiting for 1 seconds...
Done Waiting for 2 seconds...
Done Waiting for 3 seconds...
Multiprocessing can fork or spawn processes. Since Windows doesn't support fork, spawn is its only option. When spawning, a new python instance is created and must be initialized to both load the worker code and build the environment for it to execute. That include importing modules, including the script that started it all.
For this to be successful, the modules must be import safe. That is, mere import doesn't run more code than you want it to. In your case, the extra code was reasonably benign. Since you used an if
to keep many_process
from creating processes on import, all that happened is that the print that should also have been in the if
spit out incorrect information.
But really, the if
should be higher up the code than that. The create process function should not have been run at all.
main_file.py
from process1 import create_process
import time
def many_process():
global procs
for i in range(1,4):
print(f"creating process to sleep {i}")
p = create_process(i)
procs += [p]
print("all process created")
if __name__=="__main__":
# emulating windows on other platforms for test, remove in real code
import multiprocessing
multiprocessing.set_start_method("spawn")
procs = []
many_process()
for p in procs:
p.join()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.