Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send and receive large numpy arrays (several GBs) using flask

I am creating a micro-service to be used locally. From some input I am generating one large matrix each time. Right now I am using json to transfer the data but it is really slow and became the bottleneck of my application.

Here is my client side:

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)

My server is something like:

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())

How can I send large matrices ?

like image 943
user3091275 Avatar asked Oct 30 '22 11:10

user3091275


1 Answers

In the end I ended up using

np.save(matrix_path, mat)
return send_file(matrix_path+'.npy') 

On the client side I save the matrix before loading it.

like image 140
user3091275 Avatar answered Nov 09 '22 12:11

user3091275