在几台不同机器分别部署相同服务,指定IP和端口,让运维做负载均衡。
更新时,需几台机器依序关闭、修改、重启。
1. 启动服务
start_service.sh
nohup /data/zz/anaconda3/bin/python algorithms_predict.py >> ../logs/algorithms_predict.log 2>&1 &
2.关闭服务
stop_service.sh
#/bin/bash
PID=$(ps -ef|grep algorithms_predict | grep -v grep | awk '{print $2}')
if [ -z $PID ]; then
echo "process provider not exist" >> ../logs/algorithms_predict.log
exit
else
echo "process id: $PID" >> ../logs/algorithms_predict.log
kill -9 ${PID}
echo "process algorithms_predict killed" >> ../logs/algorithms_predict.log
fi
3. 打分脚本
algorithms_predict.py
from flask import Flask, request, jsonify, render_template
import lightgbm as lgb
import numpy as np
app = Flask(__name__)
lx_strangers_model = lgb.Booster(model_file='../../models/strangers_model.txt')
lx_1d_is_agree_model = lgb.Booster(model_file='../../models/lx_1d_is_agree_v9.txt')
@app.route('/lx_1d_is_agree/predict',methods=['POST', 'GET'])
def lx_1d_is_agree_predict():
data = request.get_json(force=True)
features = np.array(data['features']).reshape(1, -1)
threshold = data['threshold']
score = lx_1d_is_agree_model.predict(features)[0]
output = {}
output['uid'] = data['uid']
output['uid_f'] = data['uid_f']
output['score'] = score
output['is_agree'] = 0
if score >= threshold:
output['is_agree'] = 1
return jsonify(output)
@app.route('/lx_strangers/predict',methods=['POST', 'GET'])
def lx_strangers_predict():
data = request.get_json(force=True)
features = np.array(data['features']).reshape(1, -1)
threshold = data['threshold']
features_ = features.copy()
features_[0, -1] = 0
score0 = lx_strangers_model.predict(features_)[0]
features_[0, -1] = 1
score1 = lx_strangers_model.predict(features_)[0]
output = {}
output['uid'] = data['uid']
output['score0'] = score0
output['score1'] = score1
output['is_cancel'] = 0
if features[0, -1] == 1:
output['is_cancel'] = 1
elif score1 > score0 and score0 < threshold and score1 >= threshold:
output['is_cancel'] = 1
return jsonify(output)
def start_server():
app.run(host='0.0.0.0', port=5005, threaded=True)
if __name__ == '__main__':
start_server()