Reputation: 89
I am trying to figure out good way to register the tasks in Celery. I am using pure python and celery. I don't want to use the include or register method. Instead of that I created the separate directory for tasks with below structure.
main.py
config.py
tasks
__init__.py
add.py
So now I can specify the tasks folder as module in autodiscover_tasks. But it's throwing error as No module named 'tasks'
**main.py**
from celery import Celery
app = Celery('celery_proj')
app.config_from_object('config')
app.autodiscover_tasks(['tasks'])
app.conf.update(
task_acks_late=True,
worker_prefetch_multiplier=1,
worker_heartbeat=120,
result_expires=3600,
)
def get_app():
return app
add.py
from celery import Task,app
class TaskGeneral(Task):
def on_failure(self, exc, task_id, args, kwargs, einfo):
print('{0!r} failed: {1!r}'.format(task_id, exc))
def on_success(self, retval, task_id, args, kwargs):
print('{0!r} succeed: {1!r}'.format(task_id, retval))
@app.task(name='celery_proj.tasks.add.add', base=TaskGeneral)
def add(x, y):
return x + y
config.py
broker_url = 'redis://localhost:6379/0'
result_backend = 'redis://localhost:6379/0'
I want to know what mistake I am making and I need suggestion for the good practice like how to register the task dynamically instead of manual one.
I want to know the clear idea of good way to handle the celery when we have a long running tasks. How to register those properly
Upvotes: 1
Views: 49