Skip to main content

Easy to use object-oriented thread pool framework

Project description

Title:

Easy to use object-oriented thread pool framework

Author:

Christopher Arndt

Version:
1.2.6
Date:
2009-10-06
License:

MIT License

Description

A thread pool is an object that maintains a pool of worker threads to perform time consuming operations in parallel. It assigns jobs to the threads by putting them in a work request queue, where they are picked up by the next available thread. This then performs the requested operation in the background and puts the results in another queue.

The thread pool object can then collect the results from all threads from this queue as soon as they become available or after all threads have finished their work. It’s then possible to define callbacks to handle each result as it comes in.

Basic usage

>>> pool = ThreadPool(poolsize)
>>> requests = makeRequests(some_callable, list_of_args, callback)
>>> [pool.putRequest(req) for req in requests]
>>> pool.wait()

See the end of the module source code for a longer, annotated usage example.

Documentation

You can view the API documentation, generated by epydoc, here:

API documentation

The documentation is also packaged in the distribution.

Download

You can download the latest version of this module here:

Download directory

or see the colorized source code:

threadpool.py

You can also install it from the Cheeseshop via easy_install:

[sudo] easy:install threadpool

Or you can check out the latest development version from the Subversion repository:

svn co svn://svn.chrisarndt.de/projects/threadpool/trunk threadpool

Discussion

The basic concept and some code was taken from the book “Python in a Nutshell” by Alex Martelli, copyright O’Reilly 2003, ISBN 0-596-00188-6, from section 14.5 “Threaded Program Architecture”. I wrapped the main program logic in the ThreadPool class, added the WorkRequest class and the callback system and tweaked the code here and there.

There are some other recipes in the Python Cookbook, that serve a similar purpose. This one distinguishes itself by the following characteristics:

  • Object-oriented, reusable design

  • Provides callback mechanism to process results as they are returned from the worker threads.

  • WorkRequest objects wrap the tasks assigned to the worker threads and allow for easy passing of arbitrary data to the callbacks.

  • The use of the Queue class solves most locking issues.

  • All worker threads are daemonic, so they exit when the main programm exits, no need for joining.

  • Threads start running as soon as you create them. No need to start or stop them. You can increase or decrease the pool size at any time, superfluous threads will just exit when they finish their current task.

  • You don’t need to keep a reference to a thread after you have assigned the last task to it. You just tell it: “don’t come back looking for work, when you’re done!”

  • Threads don’t eat up cycles while waiting to be assigned a task, they just block when the task queue is empty (though they wake up every few seconds to check whether they are dismissd).

Notes

Due to the parallel nature of threads, you have to keep some things in mind:

  • Do not use simultaneous threads for tasks were they compete for a single, scarce resource (e.g. a harddisk or stdout). This will probably be slower than taking a serialized approach.

  • If you call ThreadPool.wait() the main thread will block until _all_ results have arrived. If you only want to check for results that are available immediately, use ThreadPool.poll().

  • The results of the work requests are not stored anywhere. You should provide an appropriate callback if you want to do so.

References

There are several other recipes similar to this module in the Python Cookbook, for example:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

threadpool-1.2.6.zip (72.6 kB view details)

Uploaded Source

threadpool-1.2.6.tar.bz2 (33.9 kB view details)

Uploaded Source

Built Distribution

threadpool-1.2.6-py2.6.egg (12.6 kB view details)

Uploaded Source

File details

Details for the file threadpool-1.2.6.zip.

File metadata

  • Download URL: threadpool-1.2.6.zip
  • Upload date:
  • Size: 72.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for threadpool-1.2.6.zip
Algorithm Hash digest
SHA256 9d025b0e7a775bac74e9ee11eaa1b656e12df35a3bfc82d01be2f71c957590cb
MD5 400109f00b9b069aef11627b2dd63631
BLAKE2b-256 bf93fd91ec3abda0edaff3a760e4a6d33215fbdfad60e176f3ab36e4386c1229

See more details on using hashes here.

File details

Details for the file threadpool-1.2.6.tar.bz2.

File metadata

  • Download URL: threadpool-1.2.6.tar.bz2
  • Upload date:
  • Size: 33.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for threadpool-1.2.6.tar.bz2
Algorithm Hash digest
SHA256 cc6ab9ae883b0b3c355d55e54ca1f16eebfce46f283ad7961250a3875dbec1d3
MD5 8f441ed38079762c237ee0aa5eb2d15c
BLAKE2b-256 78a87beef38095f23d04541915d0bfc46a2b0b2fa7a1f583b86f245fce4ad336

See more details on using hashes here.

File details

Details for the file threadpool-1.2.6-py2.6.egg.

File metadata

File hashes

Hashes for threadpool-1.2.6-py2.6.egg
Algorithm Hash digest
SHA256 a7e5eaa7bb4b96bd7d2983b4e4dfc725b5644dd5c1252475b4ba7b778d601019
MD5 24b100923502fb292bb0ec051b3482d3
BLAKE2b-256 9fef0982c83c935677f3b00b9d166b8c10b9b0bc9d6c162625b0bc91e10c971c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page