“multiprocessing.pool.Pool"中“context"参数的含义是什么? [英] What is the meaning of `context` argument in `multiprocessing.pool.Pool`?

查看:134
本文介绍了“multiprocessing.pool.Pool"中“context"参数的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

contextclass multiprocessing.pool.Pool 的构造函数中的一个可选参数.文档只说:

context is an optional argument in the constructor of class multiprocessing.pool.Pool. Documentation only says:

context 可用于指定用于启动工作进程的上下文.通常使用上下文对象的函数 multiprocessing.Pool()Pool() 方法创建池.在这两种情况下,上下文都设置适当.

context can be used to specify the context used for starting the worker processes. Usually a pool is created using the function multiprocessing.Pool() or the Pool() method of a context object. In both cases context is set appropriately.

它没有阐明什么是上下文对象",为什么 class Pool 构造函数需要它,以及在上述场景中它被适当设置"意味着什么.

It doesn't clarify what a "context object" is, why class Pool constructor needs it, and what it means that it "is set appropriately" in the mentioned scenarios.

推荐答案

根据平台的不同,multiprocessing 支持三种启动进程的方式.这些启动方法是:

Depending on the platform, multiprocessing supports three ways to start a process. These start methods are:

  • 生成:

父进程启动一个新的python解释器进程.
在 Unix 和 Windows 上可用.Windows 上的默认设置.

The parent process starts a fresh python interpreter process.
Available on Unix and Windows. The default on Windows.

叉子:

父进程使用 os.fork() fork Python 解释器.仅在 Unix 上可用.Unix 上的默认设置.

The parent process uses os.fork() to fork the Python interpreter. Available on Unix only. The default on Unix.

分叉服务器

当程序启动并选择forkserver启动方式时,启动一个服务器进程.从那时起,每当需要一个新进程时,父进程就会连接到服务器并请求它派生一个新进程.fork 服务器进程是单线程的,因此使用 os.fork() 是安全的.不会继承不必要的资源.

When the program starts and selects the forkserver start method, a server process is started. From then on, whenever a new process is needed, the parent process connects to the server and requests that it fork a new process. The fork server process is single threaded so it is safe for it to use os.fork(). No unnecessary resources are inherited.

在支持通过 Unix 管道传递文件描述符的 Unix 平台上可用.

Available on Unix platforms which support passing file descriptors over Unix pipes.

要选择启动方法,请在主模块的 if __name__ == '__main__' 子句中使用 set_start_method().例如:

To select a start method you use the set_start_method() in the if __name__ == '__main__' clause of the main module. For example:

import multiprocessing as mp

def foo(q):
    q.put('hello')

if __name__ == '__main__':
    mp.set_start_method('spawn')
    q = mp.Queue()
    p = mp.Process(target=foo, args=(q,))
    p.start()
    print(q.get())
    p.join()

或者,您可以使用 get_context() 获取上下文对象.Context 对象与 multiprocessing 模块具有相同的 API,并允许在同一个程序中使用多个启动方法.

Alternatively, you can use get_context() to obtain a context object. Context objects have the same API as the multiprocessing module, and allow one to use multiple start methods in the same program.

import multiprocessing as mp

def foo(q):
    q.put('hello')

if __name__ == '__main__':
    ctx = mp.get_context('spawn')
    q = ctx.Queue()
    p = ctx.Process(target=foo, args=(q,))
    p.start()
    print(q.get())
    p.join()

这就是上下文对象的来源!

This is where the context object came from!

这篇关于“multiprocessing.pool.Pool"中“context"参数的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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