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 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:
# 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.2.tar.gz
(5.5 kB
view details)
Built Distribution
File details
Details for the file threadlet-0.1.2.tar.gz
.
File metadata
- Download URL: threadlet-0.1.2.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 | 5158b207296d8903b043a9b4f43c52ad5376684564c5bd4d8e20f8158092d61c |
|
MD5 | 29fd6dc422a31b11039bc083e0725628 |
|
BLAKE2b-256 | 1f07d80c582528e88f182c2f77607f9fab839c9258e82ba451e0219935d080c9 |
File details
Details for the file threadlet-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: threadlet-0.1.2-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 | a6d3a4a23653ff81079978491425d6ee5ffeafb63bd8a446be07c6389698e6ac |
|
MD5 | 94d27df7ddea9f8171aa4da7ff87b7f9 |
|
BLAKE2b-256 | 77fa787d22c626cb19db8c2b13e4217a3cc81e799ac44ac5927ceee38051ce03 |