Improved `Thread` and `ThreadPoolExecutor` classes.
Project description
Threadlet
Improved Thread
and ThreadPoolExecutor
classes.
Installation
pip3 install threadlet
Features
- Threads with results:
from threadlet.thread import Thread
# threads now have "future" attribute of type concurrent.futures.Future.
# usage:
thread = Thread(target=sum, args=([1, 1],))
thread.start()
try:
assert thread.future.result(1) == 2
finally:
thread.join() # pay attention that "future" attribute won't be available after joining
# thread.future.result(1) # raises AttributeError
# equals to:
with Thread(target=sum, args=([1, 1],)) as thread:
assert thread.future.result(1) == 2
# equals to:
with Thread.submit(sum, [1, 1]) as thread:
assert thread.future.result(1) == 2
- ThreadPoolExecutor with improved workers performance (fixed IDLE semaphore) and new features:
import time
import threading
from threadlet.executor import ThreadPoolExecutor
MAX_WORKERS = 4
MIN_WORKERS = 2
WORK_TIME = 0.5
IDLE_TIMEOUT = 1
# "idle_timeout" argument:
# workers now are going to die after not doing any work 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:
# this amount of workers 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
- Free software: MIT license
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-0.1.1.tar.gz
(5.5 kB
view details)
Built Distribution
File details
Details for the file threadlet-0.1.1.tar.gz
.
File metadata
- Download URL: threadlet-0.1.1.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.11 CPython/3.9.7 Linux/5.11.0-46-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | efd12b73af5781e36c063daf57dcace8c2f831f7277da3d7afff711a3dd202b3 |
|
MD5 | c283ed0495fd7485d14eb3abb25c5fa1 |
|
BLAKE2b-256 | 55b40043bf075c96425ee096105f8b446a8fab13c793b673e19922f8d1f7fc3e |
File details
Details for the file threadlet-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: threadlet-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.11 CPython/3.9.7 Linux/5.11.0-46-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 353834601b175d9ab590862f798c973caa6568ceae0393de91e74a3e4097ec8e |
|
MD5 | cae544d843ea50a5947ef247d950d056 |
|
BLAKE2b-256 | 7a46b9892b9d04532c8080221c77e813b6a78c9fb33b9960fb37aa11c951dd34 |