Introduction
Provides an easy way to manage a pool of threads and submit and reap tasks run by those threads.
Examples
Generation of random numbers (known count):
import random
from threadpool import ThreadPool
tp = ThreadPool(5)
for i in range(100):
tp.add(i, random.random)
results = [tp.reap() for i in range(100)]
print("\n".join(map(str, results)))
from threadpool import ThreadPool
tp = ThreadPool(5)
for i in range(100):
tp.add(i, random.random)
results = [tp.reap() for i in range(100)]
print("\n".join(map(str, results)))
Generation of random numbers (unknown count):
import random
from threadpool import ThreadPool
tp = ThreadPool(5)
for i in range(100):
tp.add(i, random.random)
tp.drain()
results = []
while not tp.is_empty():
res = tp.reap()
results.append(res)
print("\n".join(map(str, results)))
from threadpool import ThreadPool
tp = ThreadPool(5)
for i in range(100):
tp.add(i, random.random)
tp.drain()
results = []
while not tp.is_empty():
res = tp.reap()
results.append(res)
print("\n".join(map(str, results)))
Notes:
- assuming enough tasks and a small number of workers, tp.drain() will interrupt the processing of tasks so that they number of completed tasks is not known
- tp.is_empty() checks for waiting, running, and done tasks
About
Requirements: Python
License: BSD-3
Repository: https://bitbucket.org/johnmdev/threadpool
Email: expldotinfo@gmail.com
Activity
Unknown macro: recently-updated.