18

I have a multiprocessing.Manager object that contains a multiprocessing.Queue to manage all of the work that I want a group of processes to do. I would like to get the number of elements left in this queue and was wondering how to do this?

Python's inbuilt len() function does not work.

lachy
  • 1,833
  • 3
  • 18
  • 27
  • Related question: [python queue get size, use qsize() or len()?](https://stackoverflow.com/questions/20647274/python-queue-get-size-use-qsize-or-len) – user202729 Aug 14 '21 at 03:15

1 Answers1

29

If the queue you are talking about is multiprocessing.Queue, try to use qsize() method for multiprocessing.Queue objects, but be careful:

qsize()

Return the approximate size of the queue. Because of multithreading/multiprocessing semantics, this number is not reliable.

Note that this may raise NotImplementedError on Unix platforms like Mac OS X where sem_getvalue() is not implemented.

Michael Ohlrogge
  • 10,559
  • 5
  • 48
  • 76
Shane
  • 2,231
  • 1
  • 13
  • 17
  • 14
    What is the solution to NotImplementedError – CodeGuru Jun 12 '18 at 05:51
  • 2
    One way to work around `NotImplementedError` is to define your custom Queue class that allows `.qsize()` method. Here is one working example in Python3: https://gist.github.com/FanchenBao/d8577599c46eab1238a81857bb7277c9 – Fanchen Bao Dec 23 '19 at 18:18