我正在运行一个Flask应用程序,用于检索客户端发布的图像。检索图像所需的时间取决于图像大小,这是可以理解的。但是,检索74 kb图像需要将近4秒,而检索1.1 MB需要60秒,这会让我的代码变慢。
我附加服务器端代码,其中帖子以json格式完成:
from flask import Flask, url_for
from flask import request
from flask import json
import time
app = Flask(__name__)
@app.route('/api', methods = ['POST'])
def api_message():
print(request.headers["Content-Type"])
if request.headers["Content-Type"] == "application/json":
start_time = time.time()
image = request.json['requests'][0]['image']['content']
elapsed_time = time.time() - start_time
print("Elasped time for serial is: ", elapsed_time, " seconds")
# image_type = str(type(image))
data_json = {
'exec_id': '000',
'image_type': 'list'
}
return json.dumps(data_json)
if __name__ == '__main__':
app.run(host='0.0.0.0',debug=False)
客户端请求:
import requests
import json
import sys, time
import cv2
def main():
image_file = '0119_right.png'
image = cv2.imread(image_file)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_list = image.tolist()
url = 'http://18.90.213.40:5000/api' # the original url is not provided; but can be provided if asked.
body = {
'requests': [{
'image': {
'type':'py_list',
'content': image_list
},
'hrtf': {
'type':'sofa',
'resolution': 1024
},
'ping': 'True'
}]
}
headers = {'Content-Type': 'application/json'}
r = requests.post(url, data=json.dumps(body), headers=headers)
print('---------------------------------------------------------------')
print("Response: ", r.status_code)
if __name__ == '__main__':
start_time = time.time()
main()
elapsed_time = time.time() - start_time
print("Elasped time for serial is: ", elapsed_time, " seconds")
附件是使用的图像:Image for testing
我发现的瓶颈在image = request.json['requests'][0]['image']['content']
,读取262K图像大约需要4.5秒。
我在AWS机器上运行我的服务器。有/无nginx + uswgi设置都是一样的。我试过apache。性能速度仍然相同。
有没有人知道是否有更好的方法来检索Flask应用中的图像或如何解决瓶颈?