我正在用Python Flask尝试芹菜。
celery_example.py
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)
flask_celery.py
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 服务器已启动并运行良好,我可以调用定义的路由。但是我无法启动芹菜工人(芹菜工人-A celery_example.芹菜--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
好的,
所以问题出在环境上。
我通过"virtualenv env"创建环境,它使用python创建环境,而我需要python3环境。
因此,我使用"python3 -m venv env"创建了虚拟环境,然后加载了解决错误的所有依赖项。