我有这段代码在我的机器上本地执行大约 9.2 秒。将其部署到 AWS ec2 盒子后,执行时间增加到 20 秒。
由于我之前没有实现任何缓存,我想知道我是否能够缓存特定函数的响应,或者我需要缓存我从外部获得的每个 POST。
本质上,我要的是如何缓存 POST 的结果,所以每当我再次运行它时,使用相同的参数它会使用缓存来加快响应速度。
#!flask/bin/python
from flask import Flask, jsonify
from flask import request
import json
import nltk, string
import operator
from sklearn.feature_extraction.text import TfidfVectorizer
import re
from flask import make_response
import time
import access_json
app = Flask(__name__)
def output(word_list):
start = time.time()
#RUNNING ALGORITHM *****************
comparison_words = get_synonyms(word_list) + word_list
print(comparison_words)
dict_sim = {}
for i in json_list:
dict_sim[i['jobId']] = cosine_sim(str(comparison_words), i['jobDescription'])
sorted_sim = sorted(dict_sim.items(), key=operator.itemgetter(1), reverse = True)
toplist = sorted_sim[:10]
sort_toplist = list(map(operator.itemgetter(0), toplist))
filter_toplist = (list(filter(lambda job: job['jobId'] in sort_toplist, json_list)))
#print('Our top ten suggestion from SkillzMiner are those:\n')
end = time.time()
print (end-start)
# for j in filter_toplist:
# print ('JobTitle: ' + j['jobTitle'] +
# '\nEmployer Name: ' + j['employerName'] +
# '\nLocation: ' + j['locationName'] +
# '\nReed Job URL ' + j['jobUrl'] +
# '\nJob Description: ' + cleanhtml(j["jobDescription"]))
return filter_toplist
@app.route('/postjson', methods=['POST'])
def json_handler():
content = request.get_json(force=True)
word_list = access_json.read_parsed_JSON(content)
return jsonify ({'jobs': output(word_list)})
if __name__ == '__main__':
app.run(debug=True)
app.run(host='0.0.0.0', port=80)
PS我省略了一些不相关的功能,这不是完整的代码。为术语道歉,我是这个网络服务的新手。