18,flask项目中使用celery
导包: from celery import Celery from celery.result import AsyncResult app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0' app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0' celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL']) celery.conf.update(app.config)
项目中调用:
res = get_data.delay(fp_info) get_data为耗时的程序 id =res.id async_task = AsyncResult(id=id, app=celery) result = async_task.get() # 返回的结果
@celery.task def get_data(fp): 在函数上加上修饰器
# 逻辑代码
pass
启动redis: redis-cli celery: celery -A app:celery worker --loglevel=info #app为你的flask app 启动flask: python app.py