Thread Queue

Installation

You can find this package and its source on https://pypi.python.org/pypi/ThreadQueue. To install simply use easy_install or pip:

easy_install ThreadQueue
pip install ThreadQueue

Description

The thread queue is an easy to use package that allows you to queue up runnable methods to be executed. The queue has a limited size and will block further calls to enqueue until previous threads have been terminated. This is particularly useful if you have a lot of threads to run, but only want to run a few simultaneously. An example would be a loop that should spawn threads:

>>> def some_method(some_argument):
>>>     print(some_argument)
>>> tq = ThreadQueue(5) # Only run 5 threads simultaneously
>>> for argument in range(1, 20):
>>>     tq.enqueue(some_method, argument) # This call will block if the queue is full, and will
>>>                                       # enqueue the thread as soon as space becomes available
>>> tq.join() # This call will block until all threads are done and the queue is empty

API

class threadqueue.ThreadQueue(number_of_threads=4)

An easy to use queue of threads that allows methods to be queued to be executed.

__init__(number_of_threads=4)

Creates a new thread queue with given number of maximum threads.

Parameters:number_of_threads (int) – The maximum number of threads to simultaneously run.
enqueue(runnable, *args, **kwargs)

Adds a new runnable method as a thread to the thread queue and starts executing it. If the queue is currently full, this method will block until space becomes available.

Parameters:runnable (function) – The runnable method to add to the thread queue.

You might use it as following:

>>> tq = ThreadQueue()
>>> tq.enqueue(some_method, 'hello', 'world')

This will execute some_method() in a thread, when the queue is ready.

join()

Blocks execution until all threads in the thread queue finished executing and the queue is empty.

Indices and tables

Table Of Contents

This Page