5

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 ?

user3091275
  • 1,013
  • 2
  • 11
  • 27
  • 2
    What about send the data as binary? – metmirr Feb 28 '17 at 16:33
  • Any idea of how I can do this ? How can I transform my matrix to binary and how can I receive it properly on the other end? – user3091275 Feb 28 '17 at 16:36
  • 1
    You can check python `array` module or `numpy`. Here is a question related with it http://stackoverflow.com/questions/751055/binary-array-in-python – metmirr Feb 28 '17 at 16:43

3 Answers3

1

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.

user3091275
  • 1,013
  • 2
  • 11
  • 27
0

I suppose that the problem is that the matrix takes time to generate. It's a CPU bound application

One solution would be to handle the request asynchronously. Meaning that:

  1. The server receives request and returns a 202 ACCEPTED and the link to where the client can check the progress of the creation of the matrix

  2. The client checks the returned url he either gets:

    • a 200 OK response if the matrix is not yet created
    • a 201 CREATED response if the matrix is finally created, with a link to the resource

However, Flask handles one request at a time. So you'll need to use multithreading or multiprocessing or greenthreads.

Tiwtiw
  • 465
  • 4
  • 10
0

On the client side you could do something like:

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

and on the Server side:

import numpy as np

...

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