Threadpool is an easy to use object-oriented thread pool framework written in Python. 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.
Note: This module is regarded as an extended example, not as a finished product. Feel free to adapt it too your needs.
>>> pool = ThreadPool(poolsize)
>>> requests = makeRequests(some_callable, list_of_args, callback)
>>> [pool.putRequest(req) for req in requests]
What's New in This Release: [ read full changelog ]
· Update reference to "Python In A Nutshell" to second edition (suggested by Alex Martelli).
· Fixed typo in WorkerThread.run() (thanks to Nicholas Bollweg, Aaron Levinson, Rogério Schneider, Grégory Starck for reporting).
· Fixed missing first argument in call to Queue.get() in WorkerThread.run() (thanks to Aaron Levinson for report).
· Added new argument 'do_join' to ThreadPool.dismissWorkers(). When True, the method will perform Thread.join() on each thread after dismissing it.
· Added joinAllDismissedWorkers method to ThreadPool to join dismissed threads at a later time (thanks to Aaron Levinson for patch for these two changes).