### Install via pip Source: https://github.com/celery/django-celery-results/blob/main/docs/includes/installation.txt Standard installation method using the Python Package Index. ```bash $ pip install -U django-celery-results ``` -------------------------------- ### Install from source Source: https://github.com/celery/django-celery-results/blob/main/docs/includes/installation.txt Manual installation steps for a downloaded source distribution. Requires root privileges if not using a virtual environment. ```bash $ tar xvfz django-celery-results-0.0.0.tar.gz $ cd django-celery-results-0.0.0 $ python setup.py build # python setup.py install ``` -------------------------------- ### Install development version from Git Source: https://github.com/celery/django-celery-results/blob/main/docs/includes/installation.txt Install the latest snapshot directly from the GitHub repository using pip. ```bash $ pip install https://github.com/celery/django-celery-results/zipball/master#egg=django-celery-results ``` -------------------------------- ### Install django-celery-results Source: https://github.com/celery/django-celery-results/blob/main/docs/getting_started.md Use pip to install the library into your environment. ```console $ pip install django-celery-results ``` -------------------------------- ### Track task execution duration Source: https://github.com/celery/django-celery-results/blob/main/docs/getting_started.md Enable tracking of task start times to calculate execution duration. ```python CELERY_TASK_TRACK_STARTED = True ``` -------------------------------- ### Run database migrations Source: https://github.com/celery/django-celery-results/blob/main/docs/getting_started.md Execute the migration command to create necessary database tables. ```console $ python manage.py migrate django_celery_results ``` -------------------------------- ### Run with Docker Compose Source: https://github.com/celery/django-celery-results/blob/main/README.rst Command to initialize the project environment using Docker Compose. ```bash $ docker-compose up --build ``` -------------------------------- ### Register django-celery-results in settings.py Source: https://github.com/celery/django-celery-results/blob/main/docs/getting_started.md Add the module to the INSTALLED_APPS tuple in your Django settings. ```python INSTALLED_APPS = ( ..., 'django_celery_results', ) ``` -------------------------------- ### Configure Celery result backend Source: https://github.com/celery/django-celery-results/blob/main/docs/getting_started.md Set the result backend to use the database. ```python CELERY_RESULT_BACKEND = 'django-db' ``` -------------------------------- ### Calculate task timing metrics Source: https://github.com/celery/django-celery-results/blob/main/docs/getting_started.md Retrieve task results and calculate queue waiting time and processing duration. ```python task_result = TaskResult.objects.get(task_id='xxx') waiting_time = task_result.date_started - task_result.date_created processing_time = task_result.date_done - task_result.date_started total_time = task_result.date_done - task_result.date_created print(f'result: {waiting_time=}, {processing_time=}, {total_time=}') ``` -------------------------------- ### Configure MySQL task ID length Source: https://github.com/celery/django-celery-results/blob/main/README.rst Environment variable setting to resolve OperationalError related to key length in MySQL databases. ```bash DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH=191 ``` -------------------------------- ### Configure Celery cache backend Source: https://github.com/celery/django-celery-results/blob/main/docs/getting_started.md Use the cache backend for storing task results. ```python CELERY_CACHE_BACKEND = 'django-cache' ``` -------------------------------- ### Enable extended result information Source: https://github.com/celery/django-celery-results/blob/main/docs/getting_started.md Set this flag to include additional metadata in task results. ```python CELERY_RESULT_EXTENDED = True ``` -------------------------------- ### Configure Celery with custom Django cache Source: https://github.com/celery/django-celery-results/blob/main/docs/getting_started.md Use a specific Django cache configuration for Celery results. ```python # celery setting. CELERY_CACHE_BACKEND = 'default' # django setting. CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.db.DatabaseCache', 'LOCATION': 'my_cache_table', } } ``` -------------------------------- ### django_celery_results.utils.now() Source: https://github.com/celery/django-celery-results/blob/main/docs/reference/django_celery_results.utils.md Returns the current date and time. ```APIDOC ## django_celery_results.utils.now() ### Description Returns the current date and time. ### Signature `django_celery_results.utils.now()` ``` -------------------------------- ### Injecting metadata into a Celery task Source: https://github.com/celery/django-celery-results/blob/main/docs/injecting_metadata.md Assign a dictionary to task_instance.request.meta to ensure it is stored in the TaskResult.meta field upon task execution. ```python from celery import Celery app = Celery('hello', broker='amqp://guest@localhost//') @app.task(bind=True) def hello(task_instance): task_instance.request.meta = {'some_key': 'some_value'} task_instance.update_state( state='PROGRESS', meta='Task current result' ) # If TaskResult is queried from DB at this momento it will yield # TaskResult( # result='Task current result', # meta={'some_key': 'some_value'} # some discrepancies apply as I didn't document the json parse and children data # ) return 'hello world' # After task is completed, if TaskResult is queried from DB at this momento it will yield # TaskResult( # result='hello world', # meta={'some_key': 'some_value'} # some discrepancies apply as I didn't document the json parse and children data # ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.