Multiprocessing.Queue - I want to end.

rylesny at gmail.com rylesny at gmail.com
Mon May 4 01:21:31 EDT 2009


> You may have to write the consumer loop by hand, rather than using
> 'for'.  In the same-process case, you can do this.
>
> producer:
> sentinel= object( )
>
> consumer:
> while True:
>   item= queue.get( )
>   if item is sentinel:
>     break
>   etc.
>
> Then, each consumer is guaranteed to consume no more than one
> sentinel, and thus producing one sentinel per consumer will halt them
> all.
>
> However, with multiple processes, the comparison to 'sentinel' will
> fail, since each subprocess gets a copy, not the original, of the
> sentinel.

Rather than use object() you can create a type whose instances are
equal.

class Stopper(object):
    def __eq__(self, other):
        return type(other) == type(self)

producer's stop():
  queue.put(Stopper())

consumers main loop:
  for item in iter(queue.get, Stopper()):
      ...



More information about the Python-list mailing list