我正在用Python Flask试用芹菜。
from __future__ import absolute_import, unicode_literals
from flask import Flask
from flask_celery import make_celery
flask_app = Flask(__name__)
flask_app.config.update(
CELERY_BROKER_URL='amqp://rsrc:rsrc@localhost:5672/localhost',
CELERY_RESULT_BACKEND='db+postgresql://postgres:postgres@localhost/rsrc_celery'
)
celery = make_celery(flask_app)
@flask_app.route('/process/<name>')
def process(name):
reverse.delay(name)
return "Async !"
@celery.task(name="celery_example.reverse")
def reverse(nm):
return nm[::-1]
if __name__ == "__main__":
flask_app.run(debug=True)
from __future__ import absolute_import, unicode_literals
from celery import Celery
def make_celery(app):
celery = Celery(app.import_name, backend=app.config['CELERY_RESULT_BACKEND'], broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)
TaskBase = celery.Task
class ContextTask(TaskBase):
abstract = True
def __call__(self, *args, **kwargs):
with app.app_context():
return TaskBase.__call__(self, *args, **kwargs)
celery.Task = ContextTask
return celery
FLask服务器已启动并正常运行,我可以调用已定义的路由。 但是我无法启动Celery worker( celery worker -A celery_example.celery --loglevel = info ) 这是我经常遇到的错误。
Traceback (most recent call last):
File "/usr/local/bin/celery", line 10, in <module>
sys.exit(main())
File "/Library/Python/2.7/site-packages/celery/__main__.py", line 16, in main
_main()
File "/Library/Python/2.7/site-packages/celery/bin/celery.py", line 322, in main
cmd.execute_from_commandline(argv)
File "/Library/Python/2.7/site-packages/celery/bin/celery.py", line 496, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "/Library/Python/2.7/site-packages/celery/bin/base.py", line 290, in execute_from_commandline
self.on_error(UNABLE_TO_LOAD_APP_MODULE_NOT_FOUND.format(e.name))
AttributeError: 'exceptions.ImportError' object has no attribute 'name'
--Flask_Celery
-- celery_example.py
-- flask_celery.py
-- __init__.py
答案 0 :(得分:0)
好吧,问题出在环境上。
我正在通过“ virtualenv env”创建环境,该环境使用python创建环境,而我需要python3环境。
因此,我使用“ python3 -m venv env”创建了虚拟环境,然后加载了解决错误的所有依赖项。