Queue: Which form is better/more Pythonic?
aleax at aleax.it
Sat Feb 22 21:26:15 CET 2003
Jeff Hinrichs wrote:
> I don't want to block on reading the queue since that would block the UI
> After telling you this, am I still wrong on using get_nowait() ?
Unfortunately, GUI/thread synchronization does require "busy loops"
(polling) in most cases (few GUIs have good integration with
multiple threads in their main loops). There are still right and
wrong ways to do it, and the Cookbook has a good example by Jacob
Hallén which I suggest you check.
If what you want to do is "make a burst ensuring we process all
messages arrived SO FAR on the queue" (and you do such bursts
periodically on a larger scale), I would then suggest calling
get_nowait until an Empty exception is raised, and handling the
latter with a try/except to break out of the loop -- but either of
your favourite approaches would work almost equivalently anyway,
and who cares if occasionally one message happens to end up being
processed in the next burst, it's something that's gonna happen
anyway, be it slightly more rarely or slightly more often -- it
depends on "race conditions" that are not under your control.
More information about the Python-list