Preferred way to empty multiprocessing.queue(-1) in python
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT
Music by Eric Matyas
https://www.soundimage.org
Track title: Mysterious Puzzle
--
Chapters
00:00 Preferred Way To Empty Multiprocessing.Queue(-1) In Python
01:06 Accepted Answer Score 8
02:05 Thank you
--
Full question
https://stackoverflow.com/questions/3704...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #queue #multiprocessing #pythonmultiprocessing #multiprocess
#avk47
ACCEPTED ANSWER
Score 8
Yes, you should always use the second variant: Documentation of APIs is (should) generally be more reliable than undocumented specifics of the implementation. Even if the current multiprocessing implementation works such that in your special case, get() won't block if qsize() > 0, it is not guaranteed that it will remain this way in future versions of Python, because the documentation already clearly states that it isn't.
That being said, with the current versions of Python the first version should work reliable as well as long as you have only one consuming process. Calling qsize intenally invokes sem_getvalue on Linux and WaitForSingleObjectEx on Windows; both do not lock anything. (For the Linux call, this is documented in the manpage, for the Windows call, it's a strong guess.)
Note that if you have multiple consumers and want to ensure that one of them reads the entire queue, you have to use an additional lock enclosing your loop!