Improved ThreadPoolExecutor
Project description
threadlet
Improved ThreadPoolExecutor
Table of Contents
Installation
pip install threadlet
License
threadlet
is distributed under the terms of the MIT license.
Features
import time
import threading
from threadlet import Task, Worker, ThreadPoolExecutor, wait
def calc(x):
return x * 2
# spawns new thread each time to run function in it
future = Task.run(calc, 2)
assert future.result() == 4
# spawns one thread to handle all submitted functions
with Worker() as w:
future = w.submit(calc, 3)
assert future.result() == 6
# "idle_timeout" argument (5 seconds by default):
# workers are going to die after doing nothing for "idle_timeout" seconds.
with ThreadPoolExecutor(4, idle_timeout=1) as tpe:
assert threading.active_count() == 1
fs = set()
for _ in range(100):
fs.add(tpe.submit(time.sleep, 0.1))
assert threading.active_count() == 1 + 4 # main thread + 4 workers
wait(fs)
time.sleep(1) # wait until workers die by timeout
assert threading.active_count() == 1
# "min_workers" argument:
# amount of workers which are pre-spawned at start and not going to die ever in despite of "idle_timeout".
with ThreadPoolExecutor(4, min_workers=2, idle_timeout=1) as tpe:
assert threading.active_count() == 1 + 2 # main thread + 2 pre-spawned workers
fs = set()
for _ in range(100):
fs.add(tpe.submit(time.sleep, 0.1))
assert threading.active_count() == 1 + 4 # main thread + 4 workers
wait(fs)
time.sleep(1) # wait until workers die by timeout
assert threading.active_count() == 1 + 2 # as at starting point
Benchmarks
+----------------+---------+-----------------------+-----------------------+
| Benchmark | default | threadlet | threadlet_simple |
+================+=========+=======================+=======================+
| max_workers=1 | 101 ms | 64.0 ms: 1.58x faster | 57.7 ms: 1.76x faster |
+----------------+---------+-----------------------+-----------------------+
| max_workers=2 | 97.6 ms | 65.2 ms: 1.50x faster | 55.4 ms: 1.76x faster |
+----------------+---------+-----------------------+-----------------------+
| max_workers=4 | 103 ms | 62.8 ms: 1.63x faster | 56.1 ms: 1.83x faster |
+----------------+---------+-----------------------+-----------------------+
| max_workers=8 | 95.9 ms | 63.5 ms: 1.51x faster | 57.9 ms: 1.66x faster |
+----------------+---------+-----------------------+-----------------------+
| Geometric mean | (ref) | 1.56x faster | 1.75x faster |
+----------------+---------+-----------------------+-----------------------+
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
threadlet-2.3.0.tar.gz
(7.7 kB
view hashes)
Built Distribution
Close
Hashes for threadlet-2.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 488f988c40afba57210e13b8d2569197b3b4b542856abd7545dfa85a40e3e457 |
|
MD5 | bbd23050f8e0e48b62853c8f9781d6ae |
|
BLAKE2b-256 | 5b9a25a2728f9fe841dab2b44f8509d106363c62fb6ea1b6d77ab92d3202c7a7 |