Minimalistic, embedded task manager.
Project description
Castor
Castor is a minimalistic, embedded task manager built on BeaverDB. It's designed to run background tasks (both process and thread-based) in applications where a complicated, distributed broker like Redis is overkill.
It embraces the minimalistic philosophy of BeaverDB while still being feature-full for the use cases where it makes sense.
Core Philosophy
- Explicit Over Implicit: The library avoids global state. Configuration is handled through explicit
Managerobjects, making applications more robust and testable. - Single Responsibility:
castoris a task queueing and execution library. Nothing else, nothing more. - Developer Experience: The API is designed to be intuitive, requiring minimal boilerplate to turn a function into a background task.
- Decoupled Architecture: The application that enqueues a task is fully separate from the worker process that executes it. They communicate only through the shared database file.
- Targeted Concurrency: Provides clear, mandatory choices for both I/O-bound (thread) and CPU-bound (process) concurrency models on a per-task basis.
Installation
pip install castor-io
Quickstart
Castor's API is designed to scale with your project's complexity. You can start with a simple, self-contained script and later refactor to a more robust, decoupled architecture without changing your task logic.
Mode 1: Simple (For Scripts and Prototypes)
This is the fastest way to get started. It's perfect for single-file applications, prototypes, or simple scripts where everything is in one place.
1. Create your script
In this mode, you create a Manager instance and use its @manager.task decorator to define and immediately bind your tasks.
# my_script.py
import time
from beaver import BeaverDB
from castor import Manager
# 1. Setup the manager
db = BeaverDB("tasks.db")
manager = Manager(db)
# 2. Define a background task directly on the manager instance
@manager.task(mode='thread')
def send_email(recipient: str):
"""Simulates a background I/O task."""
print(f"-> Sending email to {recipient}...")
time.sleep(2)
print(f"<- Email sent to {recipient}.")
return {"recipient": recipient, "status": "sent"}
# 3. Dispatch the task and wait for the result
if __name__ == "__main__":
print("--- Dispatching background task ---")
email_task = send_email.submit("alice@example.com")
print(f"Dispatched email task with ID: {email_task.id}")
print("\n--- Waiting for result ---")
result = email_task.join(timeout=5)
print(f"Result from email task: {result}")
2. Run the worker & your script
You'll need two separate terminal windows.
In Terminal 1, run the worker: The worker needs the import path to your manager instance.
castor my_script:manager
In Terminal 2, run your script:
python my_script.py
You will see the task being dispatched in your script and processed in the worker's log.
Mode 2: Decoupled (For Applications)
As your application grows, you'll want to separate your business logic (tasks) from your application's infrastructure (the manager). This mode is designed for maintainability, testability, and reusability.
1. Define your tasks in their own module
Use the global @task decorator. This creates unbound "blueprints" of your tasks that are not tied to any specific database or manager.
# tasks.py
import time
from castor import task
@task(mode='thread')
def send_email(recipient: str):
"""An I/O-bound task."""
print(f"-> Sending email to {recipient}...")
time.sleep(2)
return {"status": "sent"}
@task(mode='process')
def generate_report(month: str):
"""A CPU-bound task."""
print(f"-> Generating report for {month}...")
time.sleep(3)
return {"report_size_mb": 50}
2. Create and configure the manager in your main application file
The Manager discovers and binds the tasks from your tasks module, making them live.
# main.py
from beaver import BeaverDB
from castor import Manager
import tasks # Import the module containing your task blueprints
# Setup the database and manager
db = BeaverDB("prod_tasks.db")
# The manager discovers and binds the tasks from the `tasks` module.
manager = Manager(db, tasks=[tasks])
# This manager instance would typically be passed to your web framework
# or other parts of your application.
3. Dispatch tasks from anywhere
Now you can import your tasks directly and they will be correctly routed through the manager you configured.
# api.py
from tasks import send_email, generate_report
def handle_new_user_signup(email_address: str):
print("User signed up, dispatching welcome email.")
# This call is automatically routed through the configured manager
send_email.submit(email_address)
def handle_end_of_month():
print("End of month, dispatching report generation.")
generate_report.submit("September")
# In a real app, these functions would be called from web endpoints.
if __name__ == "__main__":
handle_new_user_signup("bob@example.com")
handle_end_of_month()
4. Run the worker & your application
In Terminal 1, run the worker: Point it to the configured manager in your main file.
castor main:manager -i # Using interactive mode for a nice dashboard
In Terminal 2, run your application logic:
fastapi run api:app
Features
- Dual-Mode API: Use the simple
@manager.taskdecorator for one-off scripts or the decoupled@taskdecorator for modular, scalable applications. - Execution Modes: Explicitly define tasks as
thread(for I/O-bound work) orprocess(for CPU-bound work). - Task Handle: Calling
.submit()on a task returns aTaskHandleobject, allowing you to check the.status()or wait for the result. - Scheduled and Recurring Tasks: Dispatch tasks to run at a specific time in the future using
ator after a certaindelay. Create recurring tasks that runeveryX seconds, for a specific number oftimes, oruntila certain date. - Synchronous and Asynchronous Results: Block for a result with
.join()or wait for it asynchronously with.resolve(). - Reliable Backend: Uses
beaver-dbfor a simple and reliable file-based persistence layer. - CLI Worker: A built-in command-line interface to run the worker server.
Roadmap
Castor is actively being developed. The immediate roadmap is focused on stability and developer experience:
- Retries and Error Handling: Implementing robust mechanisms for automatic task retries with configurable backoff strategies and support for dead-letter queues.
- More Examples: Adding a wider variety of examples to the documentation, showcasing how to integrate Castor with popular web frameworks and other real-world scenarios.
- Comprehensive Unit Tests: Increasing the test coverage to ensure all features are reliable and to prevent regressions.
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 castor_io-0.5.1.tar.gz.
File metadata
- Download URL: castor_io-0.5.1.tar.gz
- Upload date:
- Size: 17.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
94baed9a1d3551de1dce8d0f0be56bbd45738f0bbe2fc78b2d30d1ed540ba23f
|
|
| MD5 |
fbd2631072011234f2e7f271347231eb
|
|
| BLAKE2b-256 |
aacac48dcda06c087627dd971b4e4ea7bab9b97842d889fde8251c8771f94758
|
File details
Details for the file castor_io-0.5.1-py3-none-any.whl.
File metadata
- Download URL: castor_io-0.5.1-py3-none-any.whl
- Upload date:
- Size: 12.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e6898eaf2730246eda584b19248cb33bb2187d0e9802e610a4a93cc5b5e38f3
|
|
| MD5 |
b3e091d4b119d1eb6c991410afce9a27
|
|
| BLAKE2b-256 |
6fc3650c750bc7e1a47dbcf146cebdc9f47f8c65461c2a4036f1bda7b1945d57
|