Helps manage single runner tasks that need to avoid concurrency issues.
Project description
Django Marathon helps manage single runner tasks that need to avoid concurrency issues, ensuring that tasks are executed without overlapping.
Requirements
Python 3.6+
Django 3.1+
Installation
You can install Django Marathon via pip:
pip install django-marathon
Add marathon to your Django project’s INSTALLED_APPS:
INSTALLED_APPS = [ ... 'marathon', ]
Features
Ensures single execution of tasks to avoid concurrency.
Easy integration with Django management commands.
How Django Marathon Works
Under the hood, Django Marathon uses a simple locking mechanism to prevent concurrent execution of tasks.
1. Lock Model: Single runner tasks will create a lock in the database, using the Lock model. A lock has unique name representing the tasks. It also has a timestamp indicating when the lock was created. Each task that needs to avoid concurrency will first have to check the lock table before running.
2. SingleRunnerCommand: This is a helper class that is designed to be used in Django management commands.
3. Lock Acquisition and Release: When a command using SingleRunnerCommand is executed, it first checks if a lock with the specified name exists. If it does, the command does not run. If no lock exists, the command proceeds, and a lock is created. Once the command completes, the lock is released, allowing future executions.
Usage With Django Management Commands
Define a tasks that should be run without concurrency.
SingleRunnerCommand is a base command class provided by Django Marathon to ensure that a management command does not run concurrently.
from marathon.commands import SingleRunnerCommand from django.core.management.base import BaseCommand class Command(SingleRunnerCommand, BaseCommand): help = 'Your command description here' def handle(self, *args, **options): self.stdout.write('Starting task...') # Your task logic here self.stdout.write('Task completed.')
Using the Locking Mechanism Outside Management Commands
Django Marathon’s locking mechanism can also be utilized outside of management commands.
Here’s a general approach: - Check if the lock exists - Create a new lock if not - Implement you task logic - Release the lock
In practice, we would typically do the first 2 steps together.
Example Implementation:
from marathon.models import Lock def my_task(): lock_name = "my-unique-task-lock" # Get the existing lock or create a new one lock, created = Lock.objects.get_or_create(name=lock_name) if not created: print("Task is already running, please try again later.") return try: # Your task logic here print("Task is running...") except Exception as e: print(f"An error occurred: {str(e)}") finally: # Release the lock lock.delete() print("Task completed successfully.")
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
File details
Details for the file django_marathon-0.1.tar.gz
.
File metadata
- Download URL: django_marathon-0.1.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a9267ba6c18c86f606b210bd66b680499993b11c4ef853e1ed29433ed3ba4fc8 |
|
MD5 | 5aed0f84757159edee176ddc663d3e04 |
|
BLAKE2b-256 | c17cf72bff0a1c18b77b4ba48bb3b8fdefb1249249e516c28f3fc5b56ce2926c |
File details
Details for the file django_marathon-0.1-py3-none-any.whl
.
File metadata
- Download URL: django_marathon-0.1-py3-none-any.whl
- Upload date:
- Size: 13.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2069b9726ee7b7f6cd0643ca0e8bdbea5fb79c4ec5a3167c19225ce14acb5625 |
|
MD5 | ceebba3fb2cc800796d20bfbe9018a66 |
|
BLAKE2b-256 | c9cec3083b84472906429dec1cb4a9cbbc749aa61083a11e46cd4d8a126bf7d8 |