I try to include tasks in my webapplication but i can't start the celery work.
My project structure is the following:
website/website --> init.py, settings.py, urls.py, wsgi.py
website/index --> init.py, celery_app.py, tasks.py
Index is my startpage where i want to run the tasks.
index/celery_app.py has the following code:
from __future__ import absolute_import, unicode_literals
from celery import Celery
app = Celery('index',
broker='amqp://localhost//',
backend='amqp://',
include=['index.tasks'])
# Optional configuration, see the application user guide.
app.conf.update(
result_expires=3600,
)
if __name__ == '__main__':
app.start()
index/tasks.py :
from __future__ import absolute_import
from .celery_app import app
@app.task
def test_func():
print("Test Background Task")
When i want to start the worker with celery -A index worker --loglevel=info I get the following error: ModuleNotFoundError: No module named 'celery'. I run the commandprompt in the index folder.
The other solutions in similar questions didn't help me.
There is a problem with your file structure. Your celery app should be on the same folder as settings.py
website/website --> init.py, settings.py, urls.py, wsgi.py, celery_app.py
website/index --> init.py, tasks.py
and also in the init.py in the website folder should be having
from __future__ import absolute_import, unicode_literals
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery_app import app as celery_app
__all__ = ['celery_app']
only write
app = Celery(
"SARyS",
broker='pyamqp://guest:guest@rabbitmq.insertmendoza.com.ar',
include=["SARyS.Apps.TaskManager.Tasks"]
)
To install Celery
pip install -U Celery
or
pip install celery
To import Celery Library in Python
from celery import Celery
To Test if it works properly
app = Celery('tasks', broker='pyamqp://guest@localhost//')
@app.task
def add(x, y):
return x + y
I hope it helps.
I found the solution. The problem was the commandprompt. I couldn't use the "normal" commandprompt. Instead of the normal i opened the commandprompt of my conda environment and now it works.
For everyone who wants to know where you can find the prompt/termin:
Open Anaconda Navigator --> Environments -->Select your Environment --> Click on the Arrow --> Open Terminal and then navigate with cd (if you use windows) to the location you want.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.