我在客户端计算机上运行curl -i http://my_ip_address:5000/lzh_tw/api/v1.0/test,它将导致“ celery.exceptions.NotRegistered:'cefla.add'”。
但是,如果我再次运行它,它将正常返回“ 9”。如果我再次运行它,将导致相同的异常。一种例外,然后是一种正确的回报。我不知道背后的原因。
from flask import Flask
from celery import Celery
app = Flask(__name__)
app.config['CELERY_BROKER_URL'] = 'pyamqp://guest@localhost//'
app.config['CELERY_RESULT_BACKEND'] = 'rpc://guest@localhost//'
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)
@celery.task
def add(x, y):
return x + y
@app.route('/lzh_tw/api/v1.0/test', methods=['GET'])
def test_func():
res = add.delay(4,5)
while not res.ready():
pass
data = res.get()
return str(data)
if __name__ == '__main__':
app.run(host='0.0.0.0',debug=True)