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.

Title: Drop redundant lock in queue.Queue methods qsize(), empty() and full()
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.6
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: asvetlov, rhettinger, serhiy.storchaka, yselivanov
Priority: low Keywords:

Created on 2015-06-08 18:15 by asvetlov, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg245029 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2015-06-08 18:15
Now those methods use lock for querying queue size, like

    def qsize(self):
        with self.mutex:
            return self._qsize()

The lock is not necessary because thread context switch may be done *after* returning from mutex protected code but *before* getting result by calling side.

All three methods (qsize(), empty() and full()) gives *approximated value*, so getting rid of lock doesn't make it less stringent.
msg245045 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-06-09 05:38
It may seem pointless to hold the lock, but it is guaranteed behavior (and has been so for a very, very long time).

    # Override these methods to implement other queue organizations                                                               
    # (e.g. stack or priority queue).                                                                                             
    # These will only be called with appropriate locks held   

    # Initialize the queue representation                                                                                         
    def _init(self, maxsize):
        self.queue = deque()

    def _qsize(self):
        return len(self.queue)

    # Put a new item in the queue                                                                                                 
    def _put(self, item):

    # Get an item from the queue                                                                                                  
    def _get(self):
        return self.queue.popleft()
msg245052 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2015-06-09 09:05
Raymond, is there known custom third-party queue class derived from queue.Queue? I believe all those are in stdlib only.

Also locking guarantee is promised by comment in source code only, documentation says nothing about it.

I believe proposed change will not make any harm, but ok with closing as "wontfix" if you fill it's dangerous.
msg245059 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-06-09 10:33
Yes, there are.
Date User Action Args
2022-04-11 14:58:17adminsetgithub: 68599
2015-06-09 15:20:47rhettingersetstatus: open -> closed
resolution: not a bug
2015-06-09 10:33:06serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg245059
2015-06-09 09:05:28asvetlovsetmessages: + msg245052
2015-06-09 05:38:30rhettingersetmessages: + msg245045
versions: - Python 3.5
2015-06-09 05:33:41rhettingersetassignee: rhettinger

nosy: + rhettinger
2015-06-08 18:29:48yselivanovsetnosy: + yselivanov
2015-06-08 18:15:46asvetlovsettype: enhancement
2015-06-08 18:15:29asvetlovcreate