ThreadPools in Python 2.x

Python 2.x frameworks doesn’t come with any thread pool by default but an handy library is provided by Chris A. You can anyway easily install it with
pip install threadpool on any OS which supports pip.
You can read more about threadpool library at this address:
http://chrisarndt.de/projects/threadpool/api/

Here below you can have a look at how to use it in a simple way.

import time
from threadpool import ThreadPool, makeRequests

def worker(a):
“””thread worker function”””
print ‘Worker start’
for x in xrange(10000000):
x=x+1
print ‘Worker stop’
return

poolsize=5
pool=ThreadPool(poolsize)
data=range(10)
requests = makeRequests(worker, data)
[pool.putRequest(req) for req in requests]
pool.wait()

print “all thread finished”
# wait for 1s would be enough to get threads cleaned
time.sleep(1)

Leave a Reply

Your email address will not be published. Required fields are marked *