Based on these examples:
https://blog.balthazar-rouberol.com/celery-best-practices
https://shulhi.com/2015/10/13/class-based-celery-task/
Class based Task in django celery
I would like to create something similar:
class CalculationWorker(Task):
def __init__(self, id_user):
self._id_user = id_user
self._user = get_object_or_404(User, pk=self._id_user)
# I need to understand if the bind work or if it's needed
def _bind(self, app):
return super(self.__class__, self).bind(celery_app)
def _retrieve_some_task(self):
# long calculation
def _long_run_task(self):
# long calculation
self._retrieve_some_task()
# Main entry
def run(self):
self._long_run_task()
#
def run_job():
worker = CalculationWorker(id_user=323232)
task = worker.apply_async()
The documentation seems to say it's possible (anyway it's not clear to me) http://docs.celeryproject.org/en/latest/userguide/tasks.html#custom-task-classes. It even says:
""" This means that the init constructor will only be called once per process, and that the task class is semantically closer to an Actor. ""
but http://docs.celeryproject.org/en/latest/whatsnew-4.0.html#the-task-base-class-no-longer-automatically-register-tasks explictly says: "The best practice is to use custom task classes only for overriding general behavior, and then using the task decorator to realize the task".
As result I got a NotRegistered Exception due to this https://github.com/celery/celery/issues/3548, but adding app.tasks.register(CalculationWorker()) didn't solve it.
I'm using Django 1.10.X and Celery 4.0.0
Is that approach still valid?
Thanks