How to send and receive large numpy arrays (multiple GBs) using bulb

I am creating a microservice that will be used locally. From some input, I generate one large matrix each time. Right now I am using json to transfer data, but it is very slow and has become 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 looks something like this:

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 to send large matrices?

+4
source share
3 answers

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.

+1
source

, , .

. , :

  • 202 ACCEPTED ,

  • URL-, :

    • 200 OK, .
    • a 201 CREATED , ,

, Flask . greenthreads.

0

On the client side, you can do something like:

 with open('binariy.file', 'rb') as f:
     file = f.read()
     response = requests.post('/endpoint', data=file)

and server side:

@app.route('/endpoint', methods=['POST'])
def endpoint():
    filestr = request.data
    file = np.fromstring(filestr)
0
source

Source: https://habr.com/ru/post/1671056/


All Articles