简体   繁体   English

multiprocessing.Process 从进程创建行到行尾重复调用主 function?

[英]multiprocessing.Process calls main function from process creation line to end of line repetitively?

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?我遇到的问题是,为什么第二个文件中的行print("all process created") (第 13 行)执行了 3 次?

process1.py进程1.py

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

main_file.py主文件.py

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()

output: output:

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.由于 Windows 不支持 fork,因此 spawn 是其唯一选择。 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.生成时,会创建一个新的 python 实例,并且必须对其进行初始化以加载工作代码并构建其执行环境。 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.由于您使用if来阻止many_process在导入时创建进程,因此所发生的一切就是应该在if中的打印吐出不正确的信息。

But really, the if should be higher up the code than that.但实际上, if应该比代码更高。 The create process function should not have been run at all.创建过程 function 根本不应该运行。

main_file.py主文件.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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM