如何使用烧瓶发送和接收大型 numpy 数组(几个 GB)

use*_*275 5 json protocol-buffers flask python-3.x python-requests

我正在创建一个在本地使用的微服务。从一些输入我每次生成一个大矩阵。现在我正在使用 json 来传输数据,但它真的很慢并且成为我的应用程序的瓶颈。

这是我的客户端:

headers={'Content-Type': 'application/json'}

data = {'model': 'model_4', \
        'input': "this is my input."}

r = requests.post("http://10.0.1.6:3000/api/getFeatureMatrix", headers=headers, data=json.dumps(data))

answer = json.loads(r.text)
Run Code Online (Sandbox Code Playgroud)

我的服务器是这样的:

app = Flask(__name__, static_url_path='', static_folder='public')

@app.route('/api/getFeatureMatrix', methods = ['POST'])
def get_feature_matrix():
    arguments = request.get_json()
    #processing ... generating matrix
    return jsonify(matrix=matrix.tolist())
Run Code Online (Sandbox Code Playgroud)

如何发送大型矩阵?

use*_*275 1

最后我最终使用了

np.save(matrix_path, mat)
return send_file(matrix_path+'.npy') 
Run Code Online (Sandbox Code Playgroud)

在客户端,我在加载矩阵之前保存它。