I am beginner with Celery, and i need to save the result of the task in the database
here is tasks.py:
from __future__ import absolute_import, unicode_literals
from celery import shared_task
import hashlib
from project.celery import app
from .models import Puzzle
@shared_task(ignore_result=False)
def solve_puzzle(data, level):
# try to find a nounce value that makes the SHA-1 hash of the data and the nounce start with a certain number of zeros
solution = ""
nounce = 0
while not solution.startswith("".join(["0" for _ in range(level)])):
solution = hashlib.sha1(f"{data}{(nounce)}".encode()).hexdigest()
nounce += 1
return nounce, solution
the celery.py:
import os
from celery import Celery
# Set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
app = Celery('project', broker='redis://localhost', backend='db+sqlite:///results.sqlite')
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
app.conf.retry = 360
app.conf.timeout = 300
# Load task modules from all registered Django apps.
app.autodiscover_tasks()
settings.py:
CELERY_RESULT_BACKEND = 'django-db'
I run celery using the command:
celery -A project worker -l INFO
then got :
`[2023-09-30 19:29:13,461: INFO/MainProcess] Task puzzle.tasks.solve_puzzle[e1960235-801d-4bc4-8f5d-e7fb923830ba] received
[2023-09-30 19:29:15,909: INFO/SpawnPoolWorker-21] child process 624 calling self.run()
[2023-09-30 19:29:18,345: INFO/SpawnPoolWorker-22] child process 9428 calling self.run()`
but the Task results doesn't have the result of the rask.