python / python/cpython

Condition variable examples can be improved to show cv's main purpose - handling variables under a lock

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

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

docs
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

I think this section Condition Objects can be improved to show in examples how shared state can be handled with condition objects:

https://docs.python.org/3/library/threading.html#condition-objects

This section does mention that condition variables are supposed to be evaluated/modified under a lock, but does not give any examples on how they are supposed to be handled.

Also, worth noting that the term condition variable in original contexts, such as C++ std::condition_variable or POSIX' pthread_cond_wait, refers to the machinery that provides atomic access to some shared state, not like Python does, calling shared state as condition variables protected by condition objects.

One would typically use a condition variable for things like waiting on non-waitable conditions, like a plain in-memory queue or an app stop request. So, a condition variable could be used in order to guarantee atomic handling of both conditions like this (ignoring exceptions):

cv.acquire()
while not stop_app:
    if myq.is_empty():
        cv.wait()       # unlocks; allows other threads queue work and stop app
                        # locks again
        continue        # re-evaluate both conditions under the lock

    req = myq.get()     # myq has no locking on its own
    cv.release()

    process(req)        # expensive - must be outside the lock
    cv.acquire()
cv.release()

The queue and the application stop request represent the shared state protected by the condition variable. Something along these lines would be helpful on this page, perhaps with a better structured with block.

Also, current examples, like this one:

with cv:
    while not an_item_is_available():
        cv.wait()
    get_an_available_item()

, do not highlight that an_item_is_available may only be an in-memory operation and if something expensive is done there, it will block the app from being able to queue new work or stop.

The example should show how the work item is retrieved and how it is processed. An assignment should be made where get_an_available_item is called and same emphasis can be provided that it must be an in-memory operation that cannot block at all, not even with a timeout.

The work item that was obtained above should be passed into some kind of a processing function outside of the with cv: block, so items are processed outside of the lock.

This is just a suggestion. If you feel current description is good enough for the intended audience, go ahead and close this issue.

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

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

はじめの一歩

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

調査の方向性

Python の threading ドキュメントの「Condition Objects」セクションから始め、既存の例を報告されている共有状態のユースケースと比較します。保護されたインメモリ状態、condition ロック下での取得、ロック外での処理を示すように例を改訂します。完了の条件は、ドキュメントでロックの境界と再評価の動作が明確に説明されていることです。

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

評価

技術スタック
python
領域
documentation
issue の種類
ドキュメント
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
48/100

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

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