Celery discover tasks in files with other filenames
You could just define __all__
in your tasks/__init__.py
file. Without additional celery settings changes.
For example:
# app/tasks/__init__.py
from .file_with_tasks_1 import task1, task2
from .file_with_tasks_2 import task3
__all__ = [
'task1',
'task2',
'task3',
]
Tested on celery v4.4.6
The only reason that celery defaults to searching tasks.py is the default argument to autodiscover_tasks:
./loaders/base.py:def autodiscover_tasks(packages, related_name='tasks'):
If you use the configuration recommended by the docs you can just call autodiscover_tasks with non-default values for related_name for the different file names where you expect to have tasks. For example here is our celery.py:
from __future__ import absolute_import
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
from django.conf import settings
app = Celery('app')
# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS, related_name='tasks2')