Celery: タスクの結果をMySQLで確認する
PYPI:
django-celery-results = "^2.5.0"
settings:
| |
Database Result Backend
It seems that you are using Django and Celery to run asynchronous tasks and store the results in a database. One possible reason why the result data is always null is that you are not returning anything from your task function². For example, if your task function looks like this:
| |
Then the result data will be null because you are not returning z. To fix this, you need to add a return statement at the end of your task function:
| |
Another possible reason is that you are not using the correct backend to store the results. You need to specify the CELERY_RESULT_BACKEND setting in your settings.py file and make sure it matches the database you are using¹. For example, if you are using MySQL, you need to set something like this:
| |
You can also use django_celery_results to store the results in a Django model²³. In that case, you need to install django_celery_results and add it to your INSTALLED_APPS setting:
| |
Then you need to set the CELERY_RESULT_BACKEND to use the Django model:
| |
You also need to run migrations to create the table for storing the results:
| |
Then you can access the results from the TaskResult model by filtering by task_id or task_name²³. For example:
| |
I hope this helps. Do you have any other questions?
ソース: Bing との会話 2023/4/12
- (1) python - How to get celery results model (using django-celery …. https://stackoverflow.com/questions/56034038/how-to-get-celery-results-model-using-django-celery-results-within-task アクセス日時 2023/4/12.
- (2) DjangoとCeleryを使った非同期処理の結果取得までの流れ - Qiita. https://qiita.com/ridai/items/5060e806573b553a33fc アクセス日時 2023/4/12.
- (3) Django に Celery タスクキューを導入し、遅い処理を利用者に …. https://tech.torico-corp.com/blog/django-celery-handson/ アクセス日時 2023/4/12.
AsyncResult.result が null
Celery AsyncResult is a class that represents the result of a task. You can use it to check the status and result of a task by passing the task id to its constructor¹. For example:
| |
However, there are some possible reasons why the result may be always null. One reason is that you are not using a backend to store the results. You need to specify the CELERY_RESULT_BACKEND setting in your settings.py file and make sure it matches the database or message broker you are using². Another reason is that you are not setting the CELERY_TASK_TRACK_STARTED setting to True. This setting enables Celery to update the task status to STARTED when it begins execution². Otherwise, the status may remain PENDING until the result is ready³. A third reason is that you are not waiting for the task to finish before checking the result. You can use the get method of AsyncResult to block until the result is available, or use the ready method to check if the result is ready¹. For example:
| |
I hope this helps. Do you have any other questions?
ソース: Bing との会話 2023/4/12
- (1) celery.result — Celery 5.2.7 documentation. https://docs.celeryq.dev/en/stable/reference/celery.result.html アクセス日時 2023/4/12.
- (2) DjangoとCeleryを使った非同期処理の結果取得までの流れ - Qiita. https://qiita.com/ridai/items/5060e806573b553a33fc アクセス日時 2023/4/12.
- (3) python - Celery AsyncResult - Not working - Stack Overflow. https://stackoverflow.com/questions/49733948/celery-asyncresult-not-working アクセス日時 2023/4/12.