A simple threadpool
Project description
simple_threadpool
A simple threadpool, supports large iterable job queue(add on demand, compared to concurrent.futures.ThreadPoolExecutor
and multiprocessing.pool.ThreadPool
).
Install
pip install simple_threadpool
Usage
from __future__ import print_function
import random, time
from threading import current_thread
from simple_threadpool import ThreadPool
def my_worker(arg):
'''
custom worker
'''
print('%s: ' % current_thread().name, arg + 1)
time.sleep(random.random())
def large_iterable(size):
for i in range(size):
print('getting %s' % i)
yield i
def callback(result):
print(result)
# create a ThreadPool instance with 2 threads
tp = ThreadPool(my_worker, max_workers=2)
print('max_workers: %d' % tp.max_workers)
print('chunksize: %d' % tp.queue.maxsize)
# produce and send some data to the pool
print('First round:')
tp.feed([1, 2, 3, 4, 5])
print('Second round:')
tp.feed([6, 7, 8, 9, 0])
print('Large jobs:')
tp.feed(large_iterable(15))
# close the queue
tp.close()
print('Using callback (callback function will be threadsafe)')
tp = ThreadPool(my_worker, result_callback=callback)
tp.feed([1, 2, 3])
tp.close()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file simple_threadpool-1.0.1.tar.gz
.
File metadata
- Download URL: simple_threadpool-1.0.1.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86f9343cfc25b59c7e89558d9f59fe98bc0d1774aa32af1fd493c78677b2a964 |
|
MD5 | 452591eb548b3c2ee28fa4e2110232c8 |
|
BLAKE2b-256 | a43ed2ddbc22e8b5a463a82ee9bd8138e42f357c43ccd68695eb8d1395df650f |