我正在尝试创建一个Web应用,它可以从POST请求中接收输入,并根据该输入提供一些ML预测。
由于预测模型相当复杂,我不希望用户等待计算完成。相反,我将重计算委托给了Celery任务,用户稍后可以查看结果。
我正在使用简单的Flask应用程序与Celery,Redis和Flower。
我的view.py
文件:
@ns.route('predict/')
class Predict(Resource):
...
def post(self):
...
do_categorize(data)
return jsonify(success=True)
我的tasks.py
文件大致如下:
from ai.categorizer import Categorizer
categorizer = Categorizer(
model_path='category_model.h5',
tokenizer_path='tokenize.joblib',
labels_path='labels.joblib'
)
@task()
def do_categorize(data):
result = categorizer.predict(data)
print(result)
# Write result to the DB
...
我的Categorizer
类中的predict()
方法:
def predict(self, value):
K.set_session(self.sess)
with self.sess.as_default():
with self.graph.as_default():
prediction = self.model.predict(np.asarray([value], dtype='int64'))
return prediction
我这样运行Celery:
celery worker -A app.celery --loglevel=DEBUG
我最近几天遇到的问题是categorizer.predict(data)调用在执行过程中卡住了。
我尝试在post方法中运行categorizer.predict(data),它可以正常工作。但是如果将其放置在Celery任务中,则停止工作。如果我尝试进行调试,它就会在.predict()上冻结。
我的问题:
- 我如何解决这个问题?
- 工作程序是否有任何内存、CPU限制?
- Celery任务是否是进行这种重型计算的“正确”方式?
- 我该如何调试这个问题?我做错了什么?
- 在文件顶部初始化模型是否正确?