如何将 server-celery 与烧瓶一起使用

how use server-celery with flask

我在 flask 上安装了 celery-server 3.0.0 然后启动了服务器,但是当我使用服务器来 运行 python 代码背景时,我发现了这个错误。

~/Bureau$ sudo python exme.py
Traceback (most recent call last):
File "exme.py", line 2, in
from celery import Celery
ImportError: No module named celery

from flask import Flask
from celery import Celery
app = Flask(__name__)
app.config.update(
CELERY_BROKER_URL='redis://localhost:6379',
CELERY_RESULT_BACKEND='redis://localhost:6379'
)
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)
@celery.task() 
def add_together(a, b):
    return a + b
@APP.route('/test',methods=['POST'])
def test():
 try:
   result=add_together.delay(5,2) 
   return result
 except Exception as e: 

    return e

确保你已经通过 运行 pip install --upgrade celery 在你的 virtualenv 中正确安装了 celery

您可能不需要使用 sudo 来 运行 您的应用。当你使用 sudo 时,你的环境变量不会被转移,你会丢失你的 virtualenv。 运行 你的应用是这样的:

$ python exme.py