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
- ThreadPoolExecutor has improved worker performance(fixed IDLE semaphore) and new features:
import time
import threading
from src.threadlet import ThreadPoolExecutor
MAX_WORKERS = 4
MIN_WORKERS = 2
WORK_TIME = 0.5
IDLE_TIMEOUT = 1
# "idle_timeout" argument:
# workers are going to die after doing nothing for "idle_timeout" time.
with ThreadPoolExecutor(MAX_WORKERS, idle_timeout=IDLE_TIMEOUT) as tpe:
assert threading.active_count() == 1
for _ in range(2):
for _ in range(MAX_WORKERS):
tpe.submit(time.sleep, WORK_TIME)
assert threading.active_count() == MAX_WORKERS + 1
time.sleep(WORK_TIME + IDLE_TIMEOUT + 1) # wait until workers die on timeout
assert threading.active_count() == 1
# "min_workers" argument:
# amount of workers which are pre-created at start and not going to die ever in despite of "idle_timeout".
with ThreadPoolExecutor(MAX_WORKERS, min_workers=MIN_WORKERS, idle_timeout=IDLE_TIMEOUT) as tpe:
assert threading.active_count() == MIN_WORKERS + 1
for _ in range(MAX_WORKERS):
tpe.submit(time.sleep, WORK_TIME)
assert threading.active_count() == MAX_WORKERS + 1
time.sleep(WORK_TIME + MIN_WORKERS + 1) # wait until workers die on timeout
assert threading.active_count() == MIN_WORKERS + 1
Benchmarks
$ hatch run python bench.py
submit(sum, [1, 1]) max_workers=1 times=1000000:
---
concurrent.ThreadPoolExecutor time spent: 35.846524495995254 sec
threadlet.ThreadPoolExecutor time spent: 9.654531788008171 sec
submit(sum, [1, 1]) max_workers=2 times=1000000:
---
concurrent.ThreadPoolExecutor time spent: 20.530997663998278 sec
threadlet.ThreadPoolExecutor time spent: 9.87691844299843 sec
submit(sum, [1, 1]) max_workers=3 times=1000000:
---
concurrent.ThreadPoolExecutor time spent: 18.194354530991404 sec
threadlet.ThreadPoolExecutor time spent: 10.295373222994385 sec
submit(sum, [1, 1]) max_workers=4 times=1000000:
---
concurrent.ThreadPoolExecutor time spent: 17.788576000006287 sec
threadlet.ThreadPoolExecutor time spent: 9.785075725012575 sec
futures.wait(<1000000 futures>) max_workers=1
---
concurrent.ThreadPoolExecutor time spent: 4.7045610019995365 sec
threadlet.ThreadPoolExecutor time spent: 4.117530146002537 sec
futures.wait(<1000000 futures>) max_workers=2
---
concurrent.ThreadPoolExecutor time spent: 14.486180779000279 sec
threadlet.ThreadPoolExecutor time spent: 11.843326850997983 sec
futures.wait(<1000000 futures>) max_workers=3
---
concurrent.ThreadPoolExecutor time spent: 15.543228420996456 sec
threadlet.ThreadPoolExecutor time spent: 13.239023308997275 sec
futures.wait(<1000000 futures>) max_workers=4
---
concurrent.ThreadPoolExecutor time spent: 14.84883911900397 sec
threadlet.ThreadPoolExecutor time spent: 14.350311642992892 sec
submit(sum, [1, 1]) 1000000 times then futures.wait(<1000000 futures>) max_workers=1
---
concurrent.ThreadPoolExecutor time spent: 15.129414690003614 sec
threadlet.ThreadPoolExecutor time spent: 8.69144295899605 sec
submit(sum, [1, 1]) 1000000 times then futures.wait(<1000000 futures>) max_workers=2
---
concurrent.ThreadPoolExecutor time spent: 11.656284885000787 sec
threadlet.ThreadPoolExecutor time spent: 8.352584471998853 sec
submit(sum, [1, 1]) 1000000 times then futures.wait(<1000000 futures>) max_workers=3
---
concurrent.ThreadPoolExecutor time spent: 13.135249231010675 sec
threadlet.ThreadPoolExecutor time spent: 9.08623450199957 sec
submit(sum, [1, 1]) 1000000 times then futures.wait(<1000000 futures>) max_workers=4
---
concurrent.ThreadPoolExecutor time spent: 12.53088517699507 sec
threadlet.ThreadPoolExecutor time spent: 9.53293534599652 sec
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-1.0.0.tar.gz
(8.6 kB
view details)
Built Distribution
File details
Details for the file threadlet-1.0.0.tar.gz
.
File metadata
- Download URL: threadlet-1.0.0.tar.gz
- Upload date:
- Size: 8.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.23.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bfd0a9fc867373ef93fd2d37cfe54ef588c8b6512f1b2bded9eb71fd65c21905 |
|
MD5 | d554db86119fd5b451b5e613fbbedafe |
|
BLAKE2b-256 | 323785da96db24c7ef6bbb40e94f188a1e9cf64444a0f36bc8c931bb6e70d5df |
File details
Details for the file threadlet-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: threadlet-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.23.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bbce9d6208b23818b306bc80436922bffe582433e29685d381583038ce959c43 |
|
MD5 | 9185d2901cc5e30a4db2efdbfeba0f56 |
|
BLAKE2b-256 | 49c136a2da953f885179448d3d6767e096a84a5177c4d38761503538d437890d |