python / python/cpython

Allow `multiprocessing` Servers to return non-callables

未关闭
#118,231 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

topic-multiprocessing type-feature
主要语言
Python
星标
77.2k
派生
36k
PR 合并指标
PR 指标待抓取

描述

Feature or enhancement

Proposal:

The logic for the multiprocessing Server class is the following for retrieving values from an instance referenced by a proxy:

https://github.com/python/cpython/blob/7d369d471cf2b067c4d795d70b75201c48b46f5b/Lib/multiprocessing/managers.py#L273-L286

It works well the majority of the time, but it fails when trying to get something that doesn't immediately return a callable (it can probably be a bit more specific when it comes to what type of callable it is). A change like the following would yield more robust behavior from a base point of view and wouldn't require reimplementation in a subclass:

value_or_function = getattr(obj, methodname, SENTINEL) 

if value_or_function is SENTINEL:
    raise AttributeError(f"Type '{obj.__class__.__name__}' has no attribute named '{methodname}'")

if isinstance(value_of_function, typing.Callable):  
    try: 
        res = value_or_function(*args, **kwds) 
    except Exception as e: 
        msg = ('#ERROR', e) 
    else: 
        typeid = gettypeid and gettypeid.get(methodname, None) 
        if typeid: 
            rident, rexposed = self.create(conn, typeid, res) 
            token = Token(typeid, self.address, rident) 
            msg = ('#PROXY', (rexposed, token)) 
        else: 
            msg = ('#RETURN', res)
else:
    msg = ('#RETURN', value_or_function)

There are likely edge cases that the above does not account for, but a change similar to the one above will open up the use of instance member variables and getters for properties.

A value for SENTINEL (or better) would obviously have to be defined somewhere (it's not in the example).

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 Lib/multiprocessing/managers.py 中 issue 所链接的 Server 获取逻辑开始。跟踪 proxy 属性访问如何区分可调用值和返回值,包括属性缺失的情况以及 proxy 创建路径。完成标准是:无需重新实现即可获取实例成员变量和属性 getter,同时保持现有的可调用值行为不变。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
distributed-systems
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。