A Redis-like edge microservice for multi-threaded task processing
Project description
DecentralService
A Redis-like edge microservice for multi-threaded task processing, built with Python. DecentralService provides in-memory storage, task queuing, and multi-threaded processing capabilities that can be used as a standalone service or imported as a library.
Features
- Redis-like Storage: Key-value storage with TTL support
- Multi-threaded Task Processing: Configurable worker pool for concurrent task execution
- Priority Queue: Task prioritization with heap-based queue implementation
- HTTP API: RESTful API for external access
- Thread-safe Operations: All operations are thread-safe for concurrent access
- Task Handlers: Pluggable task processing system
- Statistics & Monitoring: Comprehensive metrics and status reporting
- Easy Integration: Can be used as a library or standalone service
- Custom Task IDs: Submit tasks with custom identifiers
- Overwrite Control: Control behavior when duplicate task IDs are encountered
- Task Search & Filtering: Search and list tasks with pagination
- FastAPI Integration: Complete example with FastAPI integration
Installation
- Clone the repository:
git clone <repository-url>
cd DecentralService
- Install dependencies:
pip install -r requirements.txt
- Install the package:
pip install -e .
Quick Start
As a Library
from decentral_service import DecentralService
# Create service instance
service = DecentralService(max_workers=4, enable_api=True)
# Use as context manager (recommended)
with service:
# Storage operations
service.set("key1", "value1")
service.set("key2", {"data": "complex"}, ttl=300) # 5 minutes TTL
value = service.get("key1")
print(f"Retrieved: {value}")
# Task processing
task_id = service.submit_task("echo", {"message": "Hello World!"})
print(f"Task submitted: {task_id}")
As a Standalone Service
from decentral_service import DecentralService
# Start service with API
service = DecentralService(host="0.0.0.0", port=6379, max_workers=4)
service.start()
# Service is now running and accessible via HTTP API
# Keep running until interrupted
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
service.stop()
API Reference
Storage Operations
# Set key-value pair
service.set("key", "value", ttl=300) # Optional TTL in seconds
# Get value
value = service.get("key")
# Check if key exists
exists = service.exists("key")
# Delete key
deleted = service.delete("key")
# List keys with pattern
keys = service.keys("user:*") # Wildcard support
# Get TTL
ttl = service.ttl("key") # -1 = no TTL, -2 = key doesn't exist
# Clear all data
service.flush()
Task Processing
# Submit task
task_id = service.submit_task(
task_type="math",
data={"operation": "add", "values": [1, 2, 3]},
priority=1 # Higher number = higher priority
)
# Check task status
status = service.get_task_status(task_id)
# Queue operations
queue_size = service.queue_size()
is_empty = service.queue_empty()
Custom Task Handlers
def my_handler(data, task):
# Process the data
result = {"processed": data, "task_id": task.id}
return result
# Add custom handler
service.worker_pool.add_task_handler("my_task_type", my_handler)
# Submit task with custom handler
task_id = service.submit_task("my_task_type", {"input": "data"})
Built-in Task Handlers
- default: Returns data as-is
- echo: Returns data with metadata
- delay: Sleeps for specified duration
- math: Performs arithmetic operations (add, multiply, subtract, divide)
HTTP API
When API is enabled, the service exposes REST endpoints:
Storage Endpoints
GET /storage/<key>- Get valuePUT /storage/<key>- Set value (JSON body:{"value": "...", "ttl": 300})DELETE /storage/<key>- Delete keyGET /storage/<key>/exists- Check if key existsGET /storage/<key>/ttl- Get TTLGET /storage/keys?pattern=*- List keysPOST /storage/flush- Clear all data
Task Endpoints
POST /tasks- Submit task (JSON body:{"type": "...", "data": {...}, "priority": 0})GET /queue/tasks/<task_id>- Get task statusGET /queue/size- Get queue sizeGET /queue/stats- Get queue statistics
System Endpoints
GET /health- Health checkGET /stats- Service statisticsGET /workers/stats- Worker pool statistics
Configuration
service = DecentralService(
host="localhost", # API server host
port=6379, # API server port
max_workers=4, # Number of worker threads
enable_api=True, # Enable HTTP API
log_level="INFO" # Logging level
)
Examples
Basic Usage
from decentral_service import DecentralService
with DecentralService() as service:
# Set data
service.set("config", {"debug": True, "version": "1.0"})
# Get data
config = service.get("config")
print(f"Config: {config}")
# Process task
task_id = service.submit_task("echo", {"message": "Hello"})
# Wait and check result
time.sleep(1)
status = service.get_task_status(task_id)
print(f"Task result: {status['result']}")
Advanced Task Processing
def process_user_data(data, task):
# Simulate processing
time.sleep(1)
return {
"user_id": data["id"],
"processed_at": time.time(),
"status": "completed"
}
service = DecentralService(max_workers=2)
service.worker_pool.add_task_handler("user_processing", process_user_data)
with service:
# Submit multiple tasks
for i in range(10):
service.submit_task(
"user_processing",
{"id": i, "name": f"User {i}"},
priority=i % 3
)
# Monitor progress
while service.queue_size() > 0:
print(f"Queue size: {service.queue_size()}")
time.sleep(0.5)
Performance
- Storage: O(1) average case for get/set/delete operations
- Queue: O(log n) for enqueue/dequeue with priority support
- Threading: Configurable worker pool for concurrent processing
- Memory: In-memory storage with automatic cleanup of expired keys
Thread Safety
All operations are thread-safe and can be used concurrently from multiple threads without additional synchronization.
Error Handling
The service includes comprehensive error handling:
- Task failures are retried up to a configurable limit
- Expired keys are automatically cleaned up
- Worker threads handle exceptions gracefully
- API endpoints return appropriate HTTP status codes
Monitoring
Access detailed statistics via:
service.get_stats()- Overall service statisticsservice.task_queue.get_stats()- Queue statisticsservice.worker_pool.get_stats()- Worker pool statistics- HTTP API endpoints for external monitoring
Changelog
[1.1.0] - 2025-01-11 (see detail in CHANGELOG.md)
License
This project is licensed under the MIT License.
Contributing
Contributions are welcome! Please feel free to submit issues and pull requests.
Support
For questions and support, please open an issue in the repository.
Project details
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 decentral_service-1.1.1.tar.gz.
File metadata
- Download URL: decentral_service-1.1.1.tar.gz
- Upload date:
- Size: 25.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc594d239cea9773b5a7302e1bfced015dedd8f7ebb69583922e4613292cefa7
|
|
| MD5 |
a07ff28535b95d70390b76caed4e58f0
|
|
| BLAKE2b-256 |
e9ff84311e176407ac5a04c091ee2b3498119242da0e9b5606dc5ff92b4a2b67
|
File details
Details for the file decentral_service-1.1.1-py3-none-any.whl.
File metadata
- Download URL: decentral_service-1.1.1-py3-none-any.whl
- Upload date:
- Size: 26.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
15498a3baacfb91d08a1939259f2bd6631751ed65ef84895e884e2d0c0b729eb
|
|
| MD5 |
dd32a753b900b0e4d3f6631d255150ad
|
|
| BLAKE2b-256 |
f7fe457dc9ec98aaf26c35eaef30c4827052e662850e8ea20ff887c7e6d209bd
|