API

Pool

class consumers.Pool(consumer, quantity=None, args=None, kwargs=None)

A Pool is responsible for the lifecycle of separate consumer processes and the queue upon which they consume from.

When used as a context manager, entering the context returns the pool object and exiting invokes its join() method.

Parameters:
  • consumer (callable) –

    A callable which will consume from the pool’s queue.

    A generator will be passed as the first argument of the consumer. It will continue to yield the next item from the queue until the queue is both closed and empty.

    Additional parameters may be specified with args and kwargs.

  • args (tuple) – Positional arguments to pass to the consumer function. Will take position after the generator argument provided by the Pool.
  • kwargs (dict) – Keyword arguments to pass to the consumer function.
  • quantity (int) –

    The number of consumer processes to create.

    Defaults to the number of CPUs in the system as determined by multiprocessing.cpu_count().

close()

Prevent any more items from being added into the pool’s queue and inform consumers to shutdown after the remaining items have been processed. Non-blocking.

join()

Block until the pool’s queue has drained and consumers have stopped.

Sets results.

Raises:consumers.ConsumerError – One or more of the consumers did not cleanly exit.
put(*args)

Enqueue all *args as a single item in the queue.

results

Results from the consumers.

Only available after join() has completed.

Returns:A tuple with a size of as many consumers in the pool.
Raises:consumers.PoolError – Results are not available at this time.
terminate()

Terminate the consumer processes.

Exceptions

exception consumers.ConsumerError

An exception in a consumer occurred.

exception consumers.PoolError

An error occurred accessing a pool.