我创建了一个简单的芹菜项目。结构如下:
+--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))
当我尝试启动工人或检查芹菜的状态时,我收到错误:
celery -A sm status
下面是错误:
Traceback (most recent call last):
File "c:anaconda3libsite-packagesceleryapputils.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:anaconda3librunpy.py", line 184, in _run_module_as_main
"__main__", mod_spec)
File "c:anaconda3librunpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:Anaconda3Scriptscelery.exe__main__.py", line 9, in <module>
File "c:anaconda3libsite-packagescelery__main__.py", line 14, in main
_main()
File "c:anaconda3libsite-packagescelerybincelery.py", line 326, in
main
cmd.execute_from_commandline(argv)
File "c:anaconda3libsite-packagescelerybincelery.py", line 488, in
execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "c:anaconda3libsite-packagescelerybinbase.py", line 279, in
execute_from_commandline
argv = self.setup_app_from_commandline(argv)
File "c:anaconda3libsite-packagescelerybinbase.py", line 481, in
setup_app_from_commandline
self.app = self.find_app(app)
File "c:anaconda3libsite-packagescelerybinbase.py", line 503, in
find_app
return find_app(app, symbol_by_name=self.symbol_by_name)
File "c:anaconda3libsite-packagesceleryapputils.py", line 366, in
find_app
found = sym.celery
AttributeError: module 'sm' has no attribute 'celery'
在窗口计算机上运行。Python 版本: Python 3.5.2 :: Anaconda 4.1.1 (64 位)
在您的celery.py
文件中,您是否尝试过在导入future
中添加下划线? 例如:
from __future__ import absolute_import
from celery import Celery
app = Celery("sm")
...
现在无法访问Windows,但我尝试在Ubuntu机器上运行您的应用程序。 遇到了导入错误,直到我在celery.py
中修复了future
导入。 没有捕获任何属性错误。