windows 多处理启动过多的 Python VM 实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1923706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:36:58  来源:igfitidea点击:

Multiprocessing launching too many instances of Python VM

pythonwindowsmultiprocessing

提问by Corey Goldberg

I am writing some multiprocessing code (Python 2.6.4, WinXP) that spawns processes to run background tasks. In playing around with some trivial examples, I am running into an issue where my code just continuously spawns new processes, even though I only tell it to spawn a fixed number.

我正在编写一些生成进程以运行后台任务的多处理代码(Python 2.6.4,WinXP)。在玩一些琐碎的例子时,我遇到了一个问题,我的代码只是不断地产生新的进程,即使我只告诉它产生一个固定的数字。

The program itself runs fine, but if I look in Windows TaskManager, I keep seeing new 'python.exe' processes appear. They just keep spawning more and more as the program runs (eventually starving my machine).

程序本身运行良好,但如果我查看 Windows 任务管理器,我会不断看到新的“python.exe”进程出现。随着程序运行,它们只会越来越多地产生(最终使我的机器饿死)。


For example,
I would expect the code below to launch 2 python.exe processes. The first being the program itself, and the second being the child process it spawns. Any idea what I am doing wrong?


例如,
我希望下面的代码启动 2 个 python.exe 进程。第一个是程序本身,第二个是它产生的子进程。知道我做错了什么吗?

import time
import multiprocessing


class Agent(multiprocessing.Process):
    def __init__(self, i):
        multiprocessing.Process.__init__(self)
        self.i = i

    def run(self):
        while True:
            print 'hello from %i' % self.i
            time.sleep(1)


agent = Agent(1)
agent.start()

回答by Peter Hansen

It looks like you didn't carefully follow the guidelines in the documentation, specifically this sectionwhere it talks about "Safe importing of main module".

看起来您没有仔细遵循文档中的指南,特别是本节中讨论“安全导入主模块”的部分。

You need to protect your launch code with an if __name__ == '__main__':block or you'll get what you're getting, I believe.

你需要用一个if __name__ == '__main__':块保护你的启动代码,否则你会得到你所得到的,我相信。

I believe it comes down to the multiprocessing module not being able to use os.fork() as it does on Linux, where an already-running process is basically cloned in memory. On Windows (which has no such fork()) it must run a new Python interpreter and tell it to import your main module and then execute the start/run method once that's done. If you have code at "module level", unprotected by the name check, then during the import it starts the whole sequence over again, ad infinitum

我相信这归结为多处理模块无法像在 Linux 上那样使用 os.fork() ,在 Linux 上,已经在运行的进程基本上被克隆到内存中。在 Windows(没有这样的 fork())上,它必须运行一个新的 Python 解释器并告诉它导入你的主模块,然后在完成后执行 start/run 方法。如果您在“模块级别”有代码,不受名称检查的保护,那么在导入期间它会重新启动整个序列,无限期

回答by Ross Rogers

When I run this in Linux with python2.6, I see a maximum of 4 python2.6 processes and I can't guarantee that they're all from this process. They're definitely not filling up the machine.

当我在 Linux 中使用 python2.6 运行它时,我看到最多 4 个 python2.6 进程,我不能保证它们都来自这个进程。他们绝对不会填满机器。

Need new python version? Linux/Windows difference?

需要新的python版本吗?Linux/Windows 的区别?

回答by Heikki Toivonen

I don't see anything wrong with that. Works fine on Ubuntu 9.10 (Python 2.6.4).

我看不出有什么问题。在 Ubuntu 9.10 (Python 2.6.4) 上运行良好。

Are you sure you don't have cron or something starting multiple copies of your script? Or that the spawned script is not calling anything that would start a new instance, for example as a side effect of import if your code runs directly on import?

您确定没有 cron 或启动脚本的多个副本的东西吗?或者生成的脚本没有调用任何会启动新实例的东西,例如作为导入的副作用,如果您的代码直接在导入时运行?