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
分支
35.9k
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 摘要。