Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Why are some of Queue.queue's method "unreliable"?

In the queue class from the Queue module, there are a few methods, namely, qsize, empty and full, whose documentation claims they are "not reliable".

What exactly is not reliable about them?

I did notice that on the Python docs site, the following is said about qsize:

Note, qsize() > 0 doesn’t guarantee that a subsequent get() will not block, nor will qsize() < maxsize guarantee that put() will not block.

I personally don't consider that behavior "unreliable". But is this what is meant by "unreliable," or is there some more sinister defect in these methods?

like image 647
Ram Rachum Avatar asked Aug 19 '09 17:08

Ram Rachum


People also ask

Is queue process safe python?

Queues are thread and process safe. This means that processes may get() and put() items from and to the queue concurrently without fear of a race condition. You can learn more about to how to use queues with multiple processes in the tutorial: Multiprocessing Queue in Python.

Is queue get thread safe python?

Yes, Queue is thread-safe.

Is Python queue blocking?

Yes -- if you call some_queue. get() within either the thread or the main function, the program will block there until some object as passed through the queue. You can do the same for some_queue.

Which method returns true if the queue is full in Python?

There are various functions available in this module: maxsize – Number of items allowed in the queue. empty() – Return True if the queue is empty, False otherwise. full() – Return True if there are maxsize items in the queue.


1 Answers

Yes, the docs use "unreliable" here to convey exactly this meaning: for example, in a sense, qsize doesn't tell you how many entries there are "right now", a concept that is not necessarily very meaningful in a multithreaded world (except at specific points where synchronization precautions are being taken) -- it tells you how many entries it had "a while ago"... when you act upon that information, even in the very next opcode, the queue might have more entries, or fewer, or none at all maybe, depending on what other threads have been up to in the meantime (if anything;-).

like image 155
Alex Martelli Avatar answered Sep 24 '22 08:09

Alex Martelli