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 run_in_thread, Worker, ThreadPoolExecutor, wait
def calc(x):
return x * 2
# spawns new thread each time to run function in it
future = run_in_thread(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:
# 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 | 94.7 ms | 70.6 ms: 1.34x faster | 64.7 ms: 1.46x faster |
+----------------+---------+-----------------------+-----------------------+
| max_workers=2 | 150 ms | 87.2 ms: 1.72x faster | 78.5 ms: 1.91x faster |
+----------------+---------+-----------------------+-----------------------+
| max_workers=4 | 163 ms | 99.2 ms: 1.64x faster | 95.4 ms: 1.71x faster |
+----------------+---------+-----------------------+-----------------------+
| max_workers=8 | 169 ms | 99.6 ms: 1.70x faster | 91.0 ms: 1.86x faster |
+----------------+---------+-----------------------+-----------------------+
| Geometric mean | (ref) | 1.59x faster | 1.72x faster |
+----------------+---------+-----------------------+-----------------------+
+----------------+---------+-----------------------+-----------------------+
| Benchmark | default | threadlet | threadlet_simple |
+================+=========+=======================+=======================+
| max_workers=1 | 28.5 MB | 27.7 MB: 1.03x faster | 27.2 MB: 1.05x faster |
+----------------+---------+-----------------------+-----------------------+
| max_workers=2 | 29.2 MB | 28.7 MB: 1.02x faster | 28.6 MB: 1.02x faster |
+----------------+---------+-----------------------+-----------------------+
| max_workers=4 | 29.4 MB | 28.7 MB: 1.02x faster | not significant |
+----------------+---------+-----------------------+-----------------------+
| max_workers=8 | 30.0 MB | 28.9 MB: 1.04x faster | 29.1 MB: 1.03x faster |
+----------------+---------+-----------------------+-----------------------+
| Geometric mean | (ref) | 1.03x faster | 1.03x 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.0.0.tar.gz
(7.8 kB
view details)
Built Distribution
File details
Details for the file threadlet-2.0.0.tar.gz
.
File metadata
- Download URL: threadlet-2.0.0.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.23.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 999a85eb864409498164f5f274abe232117d00bab8a69a347ce0ce1db775c289 |
|
MD5 | 414e716b8c86fa0aaea81b773e787543 |
|
BLAKE2b-256 | 09953cd2c454d3965069351864498868ab7723213dbe3f447e3112e1c2ca117a |
File details
Details for the file threadlet-2.0.0-py3-none-any.whl
.
File metadata
- Download URL: threadlet-2.0.0-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.23.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d55639954d3dc0c8e290e3aa71fb80d9b24f479cb2737b27c621356c2170085a |
|
MD5 | 03db5278ded4cb0a7c7e15247c9b72e9 |
|
BLAKE2b-256 | 5980d06a852d13ecd5ef40494862315ea8f6d1764b2f6cd1831ec49c71019c97 |