Celery项目没有属性'app'

时间:2017-05-31 14:25:50

标签: python celery

我创建了一个简单的芹菜项目。结构如下:

+--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:\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'

在Windows机器上运行。 Python版本:Python 3.5.2 :: Anaconda 4.1.1(64位)

1 个答案:

答案 0 :(得分:1)

celery.py文件中,您是否尝试在future导入中添加下划线?例如:

from __future__ import absolute_import
from celery import Celery
app = Celery("sm")
...

目前无权访问Windows,但我尝试在Ubuntu计算机上运行您的应用。进入ImportError,直到我修复了future中的celery.py导入。没有捕获任何AttributeErrors。