classification
Title: Add optional argument to Semaphore.release for releasing multiple threads
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: josh.r, neologix, pitrou, rhettinger, tim.peters
Priority: normal Keywords: patch

Created on 2011-01-22 00:15 by rhettinger, last changed 2019-08-29 08:45 by rhettinger. This issue is now closed.

Files
File name Uploaded Description Edit
sem.diff rhettinger, 2011-01-22 00:15 Patch to threading.py review
multirelease.diff rhettinger, 2014-06-22 02:33 Update patch to Py3.5. Add test and docs review
Pull Requests
URL Status Linked Edit
PR 15588 merged rhettinger, 2019-08-29 08:23
Messages (6)
msg126804 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-01-22 00:15
Call sem.release(5) would have the same effect as:

with lock:
    for i in range(5):
         sem.release()

The use case is when a single semaphore is holding up multiple threads and needs to release them all.  According to "The Little Book of Semaphores <http://greenteapress.com/semaphores/>", this is a common design pattern.

Basic patch attached.  If the proposal meets with acceptance, will add tests and a doc update.
msg135005 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-05-02 20:02
Your patch uses tabs for indentation. Otherwise, looks good on the principle.
msg221028 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-06-19 21:44
Seems good to proceed as there are no dissenters.
msg221213 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-06-22 02:33
Updated patch (with tests and docs).
msg221585 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2014-06-25 23:55
Never know whether to comment on issue itself, but just in case:

There are issues with the patch when n < 0 is passed, as n is not sanity checked, which would break the Semaphore invariant (value must be >= 0). n == 0 is also a weird value, but harmless if passed; release(0) would acquire and release the lock but otherwise act as a noop.
msg350757 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-08-29 08:45
New changeset 35f6301d68bdb0517be284421782d64407dfe72c by Raymond Hettinger in branch 'master':
bpo-10978: Semaphores can release multiple threads at a time (GH-15588)
https://github.com/python/cpython/commit/35f6301d68bdb0517be284421782d64407dfe72c
History
Date User Action Args
2019-08-29 08:45:42rhettingersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-08-29 08:45:29rhettingersetmessages: + msg350757
2019-08-29 08:32:21rhettingersetversions: + Python 3.9, - Python 3.5
2019-08-29 08:23:29rhettingersetpull_requests: + pull_request15264
2019-03-15 23:52:42BreamoreBoysetnosy: - BreamoreBoy
2014-06-25 23:55:38josh.rsetmessages: + msg221585
2014-06-25 02:20:57josh.rsetnosy: + josh.r
2014-06-22 14:04:36pitrousetnosy: + neologix
2014-06-22 02:33:55rhettingersetfiles: + multirelease.diff
versions: + Python 3.5, - Python 3.3
nosy: + tim.peters

messages: + msg221213
2014-06-19 21:44:16BreamoreBoysetnosy: + BreamoreBoy
messages: + msg221028
2011-05-02 20:02:25pitrousetnosy: + pitrou
messages: + msg135005

type: behavior -> enhancement
stage: patch review
2011-03-23 06:00:11rhettingersetassignee: rhettinger
2011-01-22 00:15:34rhettingercreate