python / python/cpython

PyErr_CheckSignals should be callable without holding the GIL

オープン
#133,465 コメント 3 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

interpreter-core topic-C-API type-feature
主要言語
Python
スター
77.2k
フォーク
36k
PR マージ指標
PR 指標を取得中

説明

Feature or enhancement

Proposal:

Compiled-code modules that implement time-consuming operations that don’t require manipulating Python objects, are supposed to call PyErr_CheckSignals frequently throughout each such operation, so that if the user interrupts the operation with control-C, it is cancelled promptly.

In the normal case where no signals are pending, PyErr_CheckSignals is cheap; however, callers must hold the GIL, and compiled-code modules that implement time-consuming operations are also supposed to release the GIL during each such operation. The overhead of reclaiming the GIL in order to call PyErr_CheckSignals,
and then releasing it again, sufficiently often for reasonable user responsiveness, can be substantial.

If my understanding of the thread-state rules is correct, PyErr_CheckSignals only needs the GIL if it has work to do. Checking whether there is a pending signal, or a pending request to run the cycle collector, requires only a couple of atomic loads. Therefore, I propose that we should restructure PyErr_CheckSignals and its close relatives (_PyErr_CheckSignals and _PyErr_CheckSignalsTstate) so that all the “do we have anything to do” checks are done in a batch before anything that needs the GIL. If any of them are true, then the function should acquire the GIL itself, repeat the check (because another thread could have stolen the event while we were waiting for the GIL), and then actually do the work, thus enabling callers to not hold the GIL.

I have already prepared and tested an implementation of this change and will submit a PR shortly.

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

Linked PRs
  • gh-133466
  • gh-135358
  • gh-140549

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

gh-133466、gh-135358、gh-140549 にリンクされた作業を確認し、その後 PyErr_CheckSignals とその関連する _PyErr_CheckSignals および _PyErr_CheckSignalsTstate を追跡してください。保留中の作業のチェックと GIL に依存する処理がどのように分離されているかを確認してください。完了の条件は、呼び出し側が GIL を保持せずにチェックでき、必要な処理では引き続き GIL を取得して再チェックすることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
c, python
領域
api, backend
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。