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?
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.
Yes, Queue is thread-safe.
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.
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.
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;-).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With