Windows 上的 python 多处理 [英] python multiprocessing on Windows

查看:47
本文介绍了Windows 上的 python 多处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 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 限定符的代码示例.我首先尝试运行其中一个,但遇到了几个错误,这些错误表明尝试在进程 init 完成之前尝试启动一个进程.根据该代码的作者,它在 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.

您的现在在主代码中. 行在 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()

这篇关于Windows 上的 python 多处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆