celery / django-celery-results

Celery result back end with django
Other
668 stars 206 forks source link

The `TaskResult` instance is created only after the task done. How to create this instance instantly after calling `.delay()` method? #330

Closed iamtekson closed 1 year ago

iamtekson commented 2 years ago

I implemented the celery with django_celery_results. It is working fine. I am calling the function with the .delay() method and it is giving me the task_id as well. But the problem is the TaskResult instance is created only after the task is done. I want this feature to overwrite or create the TaskResult instance just after the .delay() function is called. Is there any way that I can create the instance instantly after calling .delay()?

Any help will be highly appreciated. Thank you!

wigeria commented 2 years ago

I could be wrong, but from a quick skim, I think you can probably just manually create a TaskResult instance with the task-id; store_results seems to be using get_or_create. You would want to be a bit careful about any race conditions doing that might bring along though; perhaps using a lock?

https://github.com/celery/django-celery-results/blob/e92777025cfd56656cafd6ce7782cce115db45ec/django_celery_results/managers.py#L168-L169

orrefailaT commented 1 year ago

https://github.com/celery/django-celery-results/issues/130#issuecomment-583319233

The solution provided by this comment worked perfectly for me!