I've seen instances where qsize() and len() has been used to compute the size of the queue. What is the distinction between the two?
Asked
Active
Viewed 1.9k times
17
mingxiao
- 1,542
- 4
- 18
- 31
-
Related question: [python - Why is len() not implemented for Queues? - Stack Overflow](https://stackoverflow.com/questions/47585367/why-is-len-not-implemented-for-queues) – user202729 Aug 14 '21 at 03:16
2 Answers
19
For most containers, you'll want len, but Queue.Queue doesn't actually support len. This may be because it's old or because getting the length of a queue in a multithreaded environment isn't particularly useful. In any case, if you want the (approximate) size of a Queue, you want qsize.
user2357112
- 235,058
- 25
- 372
- 444
-
1len might tempt people to check for nonzero length before doing a get, assuming that the get won't block. – dstromberg Dec 18 '13 at 00:19
-
I am getting this crash on Mac while calling qsize return self._maxsize - self._sem._semlock._get_value() NotImplementedError any help ? – Venu Gopal Tewari Jun 14 '19 at 05:23
-
2
queue.qsize() doesn't return the number of bytes in the queue. It returns the number of "things" placed in the queue.
If you put 5 byte-arrays of 100 bytes in the queue, the qsize() will be 5, not 500.
Kate Orlova
- 2,899
- 5
- 10
- 29
user3145004
- 99
- 1
- 11