Windows上的python多处理

2022-01-12 00:00:00 python windows multiprocessing

问题描述

我是 python 编程的新手,需要一些帮助来理解 python 解释器流程,尤其是在多处理的情况下.请注意,我在 Windows 10 上运行 python 3.7.1.这是我的简单实验代码和输出.

I'm fairly new to python programming and need some help understanding the python interpreter flow, especially in the case of multiprocessing. Please note that I'm running python 3.7.1 on Windows 10. Here is my simple experimental code and the output.

import multiprocessing
import time


def calc_square(numbers, q):
    for n in numbers:
        q.put(n*n)
        time.sleep(0.2)

    q.put(-1)
    print('Exiting function')


print('Now in the main code. Process name is: ' + __name__)

if __name__ == '__main__':
    numbers = [2, 3, 4, 5]
    q = multiprocessing.Queue()
    p = multiprocessing.Process(target=calc_square, args=(numbers, q))

    p.start()

    while True:
        if q.empty() is False:
            nq = q.get()
            print('Message is:' + str(nq))
            if nq == -1:
                break
    print('Done')

    p.join()

Program Output:

Now in the main code. Process name is: __main__
Now in the main code. Process name is: __mp_main__
Message is:4
Message is:9
Message is:16
Message is:25
Exiting function
Message is:-1
Done

Process finished with exit code 0

我见过不包含 if name 限定符的代码示例.我首先尝试运行其中一个并遇到几个错误,这些错误表明试图在进程初始化完成之前尝试启动进程.根据该代码的作者,它可以在 linux 上工作,因为它分叉,而 Windows 则不能(我后来了解到 Windows 使用 spawn 作为进程启动方法.).

I've seen code examples that do not contain the if name qualifier. I first tried running one of those and got several errors that pointed to trying to trying to start a process before process init had completed. According to the author of that code, it works on linux because it forks, while Windows does not (I learned later that Windows uses spawn as the process start method.).

我的问题集中在为什么进程似乎执行目标之外的指令.我很惊讶地看到在目标向父级发送消息之前打印的输出的第二行.为什么子进程解释器运行目标之外的代码?它真的在目标代码之前运行该代码吗?为什么?

My question centers around why the process seems to execute instructions that are outside of the target. I was surprised to see the 2nd line of the output which printed before the target sent a message to the parent. Why does the sub-process interpreter run that code that is outside of the target? Is it really running that code before the target code? Why?


解决方案

在 Windows 上,新进程导入执行它的主脚本.这就是为什么在 Windows 上,您需要 if __name__ == '__main__': ...它会阻止 if 下的代码在每个进程中运行.

On Windows, the new processes import the main script, which executes it. This is why, on Windows, you need if __name__ == '__main__': ... it prevents code under that if from running in every process.

您的 Now in main code. 行位于 if 之外,因此它将在每个进程中运行.把它移到 if:

Your Now in main code. line is outside the if, so it will run in every process. Move it inside the if:

import multiprocessing
import time

def calc_square(numbers, q):
    for n in numbers:
        q.put(n*n)
        time.sleep(0.2)

    q.put(-1)
    print('Exiting function')

if __name__ == '__main__':
    print('Now in the main code. Process name is:', __name__)
    numbers = [2, 3, 4, 5]
    q = multiprocessing.Queue()
    p = multiprocessing.Process(target=calc_square, args=(numbers, q))
    p.start()

    while True:
        nq = q.get()
        print('Message is:', nq)
        if nq == -1:
            break

    print('Done')
    p.join()

相关文章