I have a following project directory:
JavaScript
x
5
1
azima:
2
__init.py
3
main.py
4
tasks.py
5
task.py
JavaScript
1
14
14
1
from .main import app
2
3
@app.task
4
def add(x, y):
5
return x + y
6
7
@app.task
8
def mul(x, y):
9
return x * y
10
11
@app.task
12
def xsum(numbers):
13
return sum(numbers)
14
main.py
JavaScript
1
12
12
1
from celery import Celery
2
3
app = Celery('azima', backend='redis://localhost:6379/0', broker='redis://localhost:6379/0', include=['azima.tasks'])
4
5
# Optional configuration, see the application user guide.
6
app.conf.update(
7
result_expires=3600,
8
)
9
10
if __name__ == '__main__':
11
app.start()
12
When I run a command to initialize celery workers, I get this error:
JavaScript
1
7
1
(azima_venv) brightseid@darkseid:~$ celery -A azima worker -l INFO
2
Usage: celery [OPTIONS] COMMAND [ARGS]
3
4
Error: Invalid value for '-A' / '--app':
5
Unable to load celery application.
6
Module 'azima' has no attribute 'celery'
7
But when I rename main.py
to celery.py
as it was earlier there’s no issue.
What am I missing here?
Advertisement
Answer
There are two approaches
- import your
app
toazima/__init__.py
JavaScript
1
4
1
from azima.main import app
2
3
celery = app # you can omit this line
4
You can omit the last line, celery will recognize the celery app from the import. Then you call celery -A azima worker -l INFO
- Call you application like
celery -A azima.main worker -l INFO