I have created a simple celery project. The structure is as follows:
+--parent
| +--sm
| | +--__init__.py
| | +--celery.py
| | +--celeryconfig.py
| | +--tasks.py
| +--__init__.py
| +--runner.py
| +--numlist.csv
celery.py:
from future import absolute_import
from celery import Celery
app = Celery("sm")
app.config_from_object('sm.celeryconfig')
app.conf.update(CELERY_TASK_RESULT_EXPIRES=3600, )
if __name__ == '__main__':
app.start()
celeryconfig.py:
from __future__ import absolute_import
CELERY_IMPORTS=("sm.tasks")
BROKER_URL = "amqp://guest:guest@localhost:5672//"
CELERY_RESULT_BACKEND = "localhost:5672//"
CELERY_ROUTES = {'sm.tasks.add_sub': {'queue': 'add_sub'},
'sm.tasks.multiply':{'queue':'multiply'}
}
CELERY_CREATE_MISSING_QUEUES = True
CELERY_MESSAGE_COMPRESSION = 'bzip2'
CELERYD_PREFETCH_MULTIPLIER = 1
CELERY_DEFAULT_QUEUE = 'sm_celery'
tasks.py:
from sm.celery import app
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
@app.task(queue='add_sub')
def add_sub(line):
try:
a = int(line.split(",")[0])
b = int(line.split(",")[1])
sums = a+b
subs = a-b
return (sums, subs)
except Exception as e:
logger.error("Something wrong in add_sub. Inputs are line=
{0}".format(line))
@app.task(queue='multiply')
def multiply(nos):
try:
a = nos[0]
b = nos[1]
return a*b
except Exception as e:
logger.error("Something wring in multiply. Inputs are nos=
{0}".format(nos))
I get the error when I try to start a worker or when I check status of celery:
celery -A sm status
Below is the error:
Traceback (most recent call last):
File "c:\anaconda3\lib\site-packages\celery\app\utils.py", line 361, in
find_app
found = sym.app
AttributeError: module 'sm' has no attribute 'app'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\anaconda3\lib\runpy.py", line 184, in _run_module_as_main
"__main__", mod_spec)
File "c:\anaconda3\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Anaconda3\Scripts\celery.exe\__main__.py", line 9, in <module>
File "c:\anaconda3\lib\site-packages\celery\__main__.py", line 14, in main
_main()
File "c:\anaconda3\lib\site-packages\celery\bin\celery.py", line 326, in
main
cmd.execute_from_commandline(argv)
File "c:\anaconda3\lib\site-packages\celery\bin\celery.py", line 488, in
execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "c:\anaconda3\lib\site-packages\celery\bin\base.py", line 279, in
execute_from_commandline
argv = self.setup_app_from_commandline(argv)
File "c:\anaconda3\lib\site-packages\celery\bin\base.py", line 481, in
setup_app_from_commandline
self.app = self.find_app(app)
File "c:\anaconda3\lib\site-packages\celery\bin\base.py", line 503, in
find_app
return find_app(app, symbol_by_name=self.symbol_by_name)
File "c:\anaconda3\lib\site-packages\celery\app\utils.py", line 366, in
find_app
found = sym.celery
AttributeError: module 'sm' has no attribute 'celery'
Running on a windows machine. Python version: Python 3.5.2 :: Anaconda 4.1.1 (64-bit)
In your celery.py
file, have you tried to add underscores to the future
import? eg:
from __future__ import absolute_import
from celery import Celery
app = Celery("sm")
...
Don't have access to Windows right now, but I tried running your app on an Ubuntu machine. Ran into an ImportError until I fixed the future
import in celery.py
. Didn't catch any AttributeErrors.
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.