python / python/cpython

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

Ouverte
#122,130 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

docs
Langage dominant
Python
Étoiles
77.2k
Forks
35.9k
Métriques de merge des PR
Métriques de PR en attente

Description

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.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par la section « Condition Objects » de la documentation Python sur threading et comparez l’exemple existant avec le cas d’utilisation d’un état partagé signalé. Révisez l’exemple pour montrer un état en mémoire protégé, sa récupération sous le verrou de la condition et son traitement en dehors de celui-ci ; le travail sera considéré comme terminé lorsque la documentation expliquera clairement la limite du verrou et le comportement de réévaluation.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
documentation
Type d'issue
Documentation
Difficulté
3/5
Temps estimé
1-2 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
48/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.