Django Marathon
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.")
Metadata
Release files for django-marathon 0.1
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 | Actions |
|---|---|---|---|---|
| django_marathon-0.1.tar.gz | 7.7 kB | Without provenance | Details for django_marathon-0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_marathon-0.1-py3-none-any.whl | Python 3 | none | any | Details for django_marathon-0.1-py3-none-any.whl |
Total release size: 20.9 kB
Release files / django_marathon-0.1.tar.gz
| Download URL | django_marathon-0.1.tar.gz |
|---|---|
| Size | 7.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a9267ba6c18c86f606b210bd66b680499993b11c4ef853e1ed29433ed3ba4fc8
|
|
BLAKE2b-256 checksum How to use checksums |
c17cf72bff0a1c18b77b4ba48bb3b8fdefb1249249e516c28f3fc5b56ce2926c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.11
|
Release files / django_marathon-0.1-py3-none-any.whl
| Download URL | django_marathon-0.1-py3-none-any.whl |
|---|---|
| Size | 13.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2069b9726ee7b7f6cd0643ca0e8bdbea5fb79c4ec5a3167c19225ce14acb5625
|
|
BLAKE2b-256 checksum How to use checksums |
c9cec3083b84472906429dec1cb4a9cbbc749aa61083a11e46cd4d8a126bf7d8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.11
|
Security for django-marathon 0.1
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.
To learn more about protecting yourself from malicious actors, visit our package security documentation.