A lightweight task execution system for concurrent operations in Python
Project description
pytaskexec
A lightweight task execution system for concurrent operations in Python. This package provides a simple yet powerful interface for creating and managing concurrent tasks using Python's ThreadPoolExecutor.
Features
- Easy task creation using decorators or function wrappers
- Concurrent execution using thread pools
- Flexible task management with scheduling and cancellation
- Comprehensive exception handling
- Configurable logging
- Type hints support
Installation
pip install pytaskexec
Quick Start
from pytaskexec import TaskRunner, taskify
import time
# Create a task using the decorator
@taskify
def process_data(item, delay=1):
time.sleep(delay) # Simulate work
return f"Processed {item}"
# Create a TaskRunner
with TaskRunner(max_workers=3) as runner:
# Schedule multiple tasks
task_ids = [
runner.schedule(process_data(f"item_{i}"))
for i in range(5)
]
# Get results as they complete
for tid in task_ids:
result = runner.get_result(tid)
print(result)
Advanced Usage
Creating Tasks
There are two ways to create tasks:
- Using the
@taskifydecorator:
@taskify
def my_task(x, y):
return x + y
task = my_task(1, 2) # Creates a Task object
- Using the
wrap_as_taskfunction:
from pytaskexec import wrap_as_task
def my_function(x, y):
return x + y
task = wrap_as_task(my_function, 1, 2) # Creates a Task object
Task Management
with TaskRunner(name="my_runner", max_workers=5) as runner:
# Schedule tasks
tid1 = runner.schedule(my_task(1, 2))
tid2 = runner.schedule(my_task(3, 4))
# Wait for specific tasks
runner.block(taskids=[tid1, tid2])
# Get results with timeout
try:
result = runner.get_result(tid1, timeout=5)
except concurrent.futures.TimeoutError:
runner.cancel_task(tid1)
# Cancel all pending tasks
runner.cancel_pending()
Debug Logging
Set the environment variable PYTASKEXEC_DEBUG to enable debug logging:
export PYTASKEXEC_DEBUG=1 # Linux/Mac
set PYTASKEXEC_DEBUG=1 # Windows
Development
Setting up the Development Environment
- Clone the repository:
git clone https://github.com/anandan-bs/pytaskexec.git
cd pytaskexec
- Create a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
- Install development dependencies:
pip install -r requirements.txt
Running Tests
pytest tests/
Code Style
This project uses:
- Black for code formatting
- Flake8 for linting
- MyPy for type checking
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests and ensure they pass
- Submit a pull request
License
This project is licensed under the MIT License. See the LICENSE file for details.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytaskexec-0.1.0.tar.gz.
File metadata
- Download URL: pytaskexec-0.1.0.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
70b1892c2a66fb7d05a3ec2e0c7848d371ade2303b9a164a137770bf6a3b7cba
|
|
| MD5 |
aab3d7796edae5c9eef17a57a240ecf0
|
|
| BLAKE2b-256 |
03398f2792e2b081b64cb688de19707b770afaca08421462c135cec1dc024fca
|
Provenance
The following attestation bundles were made for pytaskexec-0.1.0.tar.gz:
Publisher:
python-publish.yml on anandan-bs/pytaskexec
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytaskexec-0.1.0.tar.gz -
Subject digest:
70b1892c2a66fb7d05a3ec2e0c7848d371ade2303b9a164a137770bf6a3b7cba - Sigstore transparency entry: 220004938
- Sigstore integration time:
-
Permalink:
anandan-bs/pytaskexec@90c166b3d76e1748dd62f169450ec75d08fd4e88 -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/anandan-bs
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@90c166b3d76e1748dd62f169450ec75d08fd4e88 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pytaskexec-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pytaskexec-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a6eee9cee89f007fc6983e52a8df11ae779d7498ee1e0ea34df8c1c3741d4b70
|
|
| MD5 |
99ae691a3ca581678a09554553482365
|
|
| BLAKE2b-256 |
9341e73ec574522e954061ea5befb837935e44c051e60e94c227e7fd4bc3b593
|
Provenance
The following attestation bundles were made for pytaskexec-0.1.0-py3-none-any.whl:
Publisher:
python-publish.yml on anandan-bs/pytaskexec
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytaskexec-0.1.0-py3-none-any.whl -
Subject digest:
a6eee9cee89f007fc6983e52a8df11ae779d7498ee1e0ea34df8c1c3741d4b70 - Sigstore transparency entry: 220004942
- Sigstore integration time:
-
Permalink:
anandan-bs/pytaskexec@90c166b3d76e1748dd62f169450ec75d08fd4e88 -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/anandan-bs
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@90c166b3d76e1748dd62f169450ec75d08fd4e88 -
Trigger Event:
release
-
Statement type: