Una integración súper simple de Django & Ceyery : esta biblioteca es compatible activamente por AppSeed.
Características:
Celery TasksView LOGS y salidaMinimal Configurationusers_in_db() : enumere a todos los usuarios registradosexecute_script() : permita a los usuarios ejecutar los scripts definidos en CELERY_SCRIPTS_DIR (parámetro CFG)
Instale el paquete a través de
PIP
$ pip install django-tasks-manager
// OR
$ pip install git+https://github.com/app-generator/django-tasks-manager.gitIncluir el nuevo enrutamiento
# core/urls.py
from django . urls import path , include # <-- UPDATE: Add 'include' HELPER
urlpatterns = [
...
path ( "" , include ( "django_tm.urls" )), # <-- New Routes
...
]Crear directorios Scrips & Logs : el lugar recomendado está en la raíz del proyecto:
$ mkdir celery_scripts # Used in Settings -> CELERY_SCRIPTS_DIR
$ mkdir celery_logs # Used in Settings -> CELERY_SCRIPTS_DIRActualizar la configuración Agregar importación del objeto
osoperativo
import os # <-- NEWConfiguración de actualización : incluya las nuevas aplicaciones
INSTALLED_APPS = [
...
'django_tm' , # Django Tasks Manager # <-- NEW
'django_celery_results' , # Django Celery Results # <-- NEW
]Configuración de actualización : incluya las nuevas plantillas
TEMPLATE_DIR_TASKS = os . path . join ( BASE_DIR , "django_tm/templates" ) # <-- NEW
TEMPLATES = [
{
'BACKEND' : 'django.template.backends.django.DjangoTemplates' ,
'DIRS' : [ TEMPLATE_DIR_TASKS ], # <-- Updated
'APP_DIRS' : True ,
},
]Configuración de actualización : nueva sección CELERY_
#############################################################
# Celery configurations
# https://docs.celeryq.dev/en/stable/django/first-steps-with-django.html
# !!!
# BASE_DIR points to the ROOT of the project
# Note: make sure you have 'os' object imported
# !!!
# !!!
# BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# !!!
# Working Directories required write permission
CELERY_SCRIPTS_DIR = os . path . join ( BASE_DIR , "celery_scripts" )
CELERY_LOGS_DIR = os . path . join ( BASE_DIR , "celery_logs" )
CELERY_BROKER_URL = os . environ . get ( "CELERY_BROKER" , "redis://localhost:6379" )
CELERY_RESULT_BACKEND = os . environ . get ( "CELERY_BROKER" , "redis://localhost:6379" )
CELERY_TASK_TRACK_STARTED = True
CELERY_TASK_TIME_LIMIT = 30 * 60
CELERY_CACHE_BACKEND = "django-cache"
CELERY_RESULT_BACKEND = "django-db"
CELERY_RESULT_EXTENDED = True
CELERY_RESULT_EXPIRES = 60 * 60 * 24 * 30 # Results expire after 1 month
CELERY_ACCEPT_CONTENT = [ "json" ]
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
#############################################################
#############################################################Iniciar la aplicación
$ # Set up the database
$ python manage.py makemigrations
$ python manage.py migrate
$
$ # Create the superuser
$ python manage.py createsuperuser
$
$ # Start the application (development mode)
$ python manage.py runserver # default port 8000superuserTasks : http://127.0.0.1:8000/tasksInicie el Administrador de apio (otra terminal) y el entorno de actualización
Exportar DJANGO_SETTINGS_MODULE utilizando el valor proporcionado en manage.py
$ export DJANGO_SETTINGS_MODULE=cfg.settings El valor utilizado de exportación debe tomarse de manage.py :
def main ():
"""Run administrative tasks."""
os . environ . setdefault ( "DJANGO_SETTINGS_MODULE" , "cfg.settings" ) # <-- VALUE to be exported
try :
from django . core . management import execute_from_command_line
except ImportError as exc :
... Nota : Se espera que Redis Server en el puerto 6379 (predeterminado). En caso de que Redis se ejecute en otro PORT , actualice la configuración: CELERY_BROKER_URL y CELERY_RESULT_BACKEND .
$ celery --app=django_tm.celery.app worker --loglevel=info
View all tasks

View Task LOG

free sample que explica:Project Creation (archivos mínimos agregados)Install y generar Django CoreDjango-TM (este paquete)Update ConfigurationStart the appTasks ManagerDjango Tasks Manager - Biblioteca de código abierto proporcionada por AppSeed