[issue18078] threading.Condition to allow notify on a specific waiter

João Bernardo report at bugs.python.org
Wed May 29 16:39:54 CEST 2013


João Bernardo added the comment:

I did what @Richard Oudkerk said and created the "wait_for_any" classmethod for the Condition class.

Other Changes:

 - I had to refactor "wait" and "wait_for" to be specializations of "wait_for_any".
 - try...except on "notify" because the inner lock might have been released by other condition.
 - Added two helper functions "_remove_waiter" and "_wait" (the part of the old wait function to re-acquire the inner lock)

Bonus:
To simplify the use, I added a "from_condition" constructor to create a new condition using the same lock as an existing one.
That way, you don't need to record the lock someplace else before creating a new Condition for the same lock.


* The current tests pass.

* Missing: new tests and docs.


----

Sample:

    lock = Lock()
    cond1 = Condition(lock)
    cond2 = Condition(lock)
    cond3 = Condition.from_condition(cond1)

    with lock:
        Condition.wait_for_any({cond1: foo, cond2: bar})
        Condition.wait_for_any([cond1, cond2, cond3]) # no predicates
                                                      # used on "wait"
    with cond2: # same thing
        Condition.wait_for_any({cond3: foo})

---

PS: the patch text is messy because of refactoring and some lines were moved. It is easy to read when applied.

----------
keywords: +patch
Added file: http://bugs.python.org/file30413/wait_for_any.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18078>
_______________________________________


More information about the Python-bugs-list mailing list