Autothread
Parallelization made easy.
Autothread contains a collection of decorators that make it as easy as possible to add threading or multiprocessing to your projects. Autothread has two types of decorators: blocking and non-blocking.
Non-blocking
Autothreads non-blocking decorators are the easiest way to add threading/multiprocessing to your project. You just need to add a single decorator, which changes your function to calculate in the background instead of blocking the script.
import autothread
import time
from time import sleep as heavyworkload
@autothread.async_threaded() # <-- This is all you need to add
def example(x, y) -> int:
heavyworkload(1)
return x*y
start = time.time()
results = []
for i in range(5):
results.append(example(i, 10)) # the thread is started
print(results) # autothread waits for the thread to end and gives you the result
print("Time expired: ", time.time()-start)
>>> [0, 10, 20, 30, 40]
Time expired: 1.002363681793213
autothread.async_processed works in the same way but uses multiprocessing instead of threading.
More info can be found in the non-blocking README.
Blocking
The blocking decorators of autothread change the function slightly, but give you more control over when the function is executed:
import autothread
import time
from time import sleep as heavyworkload
@autothread.multithreaded() # <-- This is all you need to add
def example(x: int, y: int):
heavyworkload(1)
return x*y
@autothread.multiprocessed() # <-- Or to use multiprocessing
def example2(x: int, y: int):
heavyworkload(1)
return x*y
Now, instead of integers, your function can take lists of integers. The function will be repeated or each item in your list on a separate thread/process:
start = time.time()
result = example([1, 2, 3, 4, 5], 10)
print(result)
print("Time expired: ", time.time()-start)
>>> [10, 20, 30, 40, 50]
Time expired: 1.0041766166687012
More info can be found in the blocking README.
Installing
You can install autothread using:
pip install autothread
Or by cloning the source:
git clone https://github.com/Basdbruijne/autothread.git
cd autothread
pip install -e .
Known issues
None at the moment, please open a bug if you run into an issue.
Metadata
Release files for autothread 0.0.10
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| autothread-0.0.10.tar.gz | 14.5 kB | Without provenance | Details |
Release files / autothread-0.0.10.tar.gz
| Download URL | autothread-0.0.10.tar.gz |
|---|---|
| Size | 14.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
938aab973fdd3c85a58571d6cefc758f7f070a08a451244eedaa0f764a8807fd
|
|
BLAKE2b-256 checksum How to use checksums |
4bef9927258f0224c0f20561ec15b3b86d2373cb28cd22f664ba712a393817b1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.2
|
Security for autothread 0.0.10
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.