“如何修复”芹菜.例外.未注册: '添加“虫子?

2024-05-20 12:11:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我在我的客户机上运行curl-Ihttp://my_ip_address:5000/lzh_tw/api/v1.0/test,它将导致“芹菜.例外.未注册: '添加' ". 你知道吗

但是,如果我再次运行它,它将正常返回'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)

Tags: namefromtestimportapiconfigappflask