This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Threading Semaphore and BoundedSemaphore release method implementation improvement
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: andrei.avk, besi7dollma, kj, pitrou, rhettinger, serhiy.storchaka
Priority: normal Keywords:

Created on 2021-09-28 13:32 by besi7dollma, last changed 2022-04-11 14:59 by admin.

Messages (4)
msg402779 - (view) Author: Besart Dollma (besi7dollma) Date: 2021-09-28 13:32
Hi, 
I was looking at the implementation of Semaphore and BoundedSemaphore in threading.py and I saw that `notify` is called on a loop n times while it supports an n parameter. 

https://github.com/python/cpython/blob/84975146a7ce64f1d50dcec8311b7f7188a5c962/Lib/threading.py#L479

Unless I am missing something, removing the loop and replacing it with self._cond.notify(n) will slightly improve performance by avoiding the function call overhead.

I can prepare a Pool Request if the change is acceptable.
Thanks,
msg404461 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-10-20 14:22
Besart: with the current code, if there's a valueError when releasing a thread, it will be retried again n times, with your change, n waiting threads will be released, even if some of them fail with ValueError.

I don't know enough about threading module to say if this change should be made.
msg404570 - (view) Author: Besart Dollma (besi7dollma) Date: 2021-10-21 06:51
This is another illogical behaviour, if I am trying to release 5 threads, and I fail the releasing the first one, why should I try the first one 5 times?
msg404571 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-10-21 08:42
This code was added in bpo-10978. Raymond, what are your thoughts?
History
Date User Action Args
2022-04-11 14:59:50adminsetgithub: 89474
2021-10-21 08:42:59serhiy.storchakasetnosy: + rhettinger

messages: + msg404571
versions: + Python 3.11, - Python 3.9
2021-10-21 06:51:36besi7dollmasetmessages: + msg404570
2021-10-20 14:49:14kjsetnosy: + pitrou, serhiy.storchaka
2021-10-20 14:22:32andrei.avksetnosy: + kj
2021-10-20 14:22:08andrei.avksetnosy: + andrei.avk
messages: + msg404461
2021-09-28 13:32:03besi7dollmacreate