Python多处理流程与独立Python VM [英] Python multiprocessing process vs. standalone Python VM

查看:83
本文介绍了Python多处理流程与独立Python VM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了multiprocessing模块在使用通信资源连接进程方面的易用性之外,使用multiprocessing生成多个进程与使用subprocess启动单独的Python VM相比,还有其他区别吗? ?

Aside from the ease of use of the multiprocessing module when it comes to hooking up processes with communication resources, are there any other differences between spawning multiple processes using multiprocessing compared to using subprocess to launch separate Python VMs ?

推荐答案

在Posix平台上,multiprocessing原语实际上包装了os.fork().这意味着您在多处理中生成一个进程时,已经导入/初始化的代码仍保留在子进程中.

On Posix platforms, multiprocessing primitives essentially wrap an os.fork(). What this means is that at point you spawn a process in multiprocessing, the code already imported/initialized remains so in the child process.

如果您有很多要初始化的东西然后每个子进程本质上对这些初始化的对象(副本)执行操作,那么这可能是个福音,但是如果您在子进程中运行的事物完全不相关,则并非全部有用.

This can be a boon if you have a lot of things to initialize and then each subprocess essentially performs operations on (copies of) those initialized objects, but not all that helpful if the thing you run in the subprocess is completely unrelated.

在类unix平台上使用multiprocessing的文件句柄,套接字等资源也有影响.

There are also implications for resources such as file-handles, sockets, etc with multiprocessing on a unix-like platform.

同时,使用subprocess时,每次Popen一个新进程时,您都在创建一个全新的程序/解释器.这意味着它们之间可以共享的内存更少,但这也意味着您可以将Popen打开到一个完全独立的程序中,或者将Popen插入到同一程序中的新入口点.

Meanwhile, when using subprocess, you are creating an entirely new program/interpreter each time you Popen a new process. This means there can be less shared memory between them, but it also means you can Popen into a completely separate program, or a new entry-point into the same program.

在Windows上,multiprocessingsubprocess之间的差异较小,因为Windows不提供fork().

On Windows, the differences are less between multiprocessing and subprocess, because windows does not provide fork().

这篇关于Python多处理流程与独立Python VM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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