python / python/cpython

Multiprocessing Semaphore and BoundedSemaphore - incomplete features ('get_value' missing) on MacOSX.

未關閉
#125,828 2 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

OS-mac stdlib topic-multiprocessing type-bug
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

Bug report

Bug description:

I've been working on this PR: Adding multiprocessing queue shutdown feature for a while on my Mac machine. I've come up with a solution for this new feature and wanted to check that it also worked on Linux. However, I had forgotten that BoundedSemaphore is not fully functional on MacOSX. In fact, there's no check for exceeding the upper bound. See this example:

import multiprocessing as mp
bs = mp.BoundedSemaphore(2)
print(bs) # <BoundedSemaphore(value=unknown, maxvalue=2)>  note invalid representation string 
bs.release() # Succeed with MacOSX, Raise an exception on Linux 

I had to revise my solution because it was invalid on Linux. Such a shame, I wasted a lot of time. I checked on the documentation a few days ago . There is not a specific warning about this case.

As a reminder, the initial reason is the absence of the sem_getvalue C function in the MacOSX semaphore implementation. This prevents retrieval of the current semaphore value. It also prevents the Semaphore use as a shared counter. This issue is (very ?) old, and still hasn't been solved from Apple, could a workaround be implemented on MacOSX, so that Semaphore and BoundedSemaphore would have the same functionalities as Linux/Win.

A workaround (in Python) would be to use a shared value and to revise the acquire and release methods in order to emulate the semaphore internal counter. It is quite possible that this solution would greatly reduce performance.

I've already done a lot of tests (See the attached draft PR). And also, this solution should fix the representation strings that are currently wrong.

Before going any further, I was wondering if there's a reason why no patch attempt has been implemented ? And was there an old discussion about this already ?

Thanks for your feedback

CPython versions tested on:

3.10, 3.11, 3.12, 3.13, CPython main branch

Operating systems tested on:

macOS

Linked PRs
  • gh-125829
  • gh-130913

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 macOS 上的 multiprocessing.Semaphore 和 BoundedSemaphore 開始,重點關注缺少的 sem_getvalue 行為以及 acquire/release 路徑。查看連結的 draft PR,並將其中展示的表示形式和邊界檢查行為與 Linux 進行比較;完成這項工作需要就一種約定的 workaround 達成一致,並為值、邊界和表示形式提供 coverage。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
operating-systems
Issue 類型
缺陷
難度
5/5
預估耗時
一週以上
活躍度
停滯
描述清晰度
需要釐清
新手友好度
20/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。