Вопрос

I've built a Flask+nginx+gunicorn application that talks to a service via a pair zmq socket that in turn sends the request over to distributed workers and gets back the result.

However, I've not put a lot of checks to see that the right info is send back to the user. This means that sometimes if User A and User B request their data about the same time, the result may end up for the wrong user.

I'm guessing I need to send some context with the request (like a username). When the result comes back, put it in a queue and somehow make sure that the browser request picks the right result based on context.

How would you go about to make sure that the data is sent to its rightful owner?

The code looks like this:

@app.route('/restart', methods = ['POST'])
def restart():
    uuid = request.form['uuid']
    msg = json.dumps({'command': 'restart', 'uuid': uuid})
    send_to_master(msg)
    resp = Response(response=data, status=200, mimetype="application/json")
    return resp

def send_to_master(msg):
    context = zmq.Context()
    s = context.socket(zmq.PAIR)
    s.connect("tcp://localhost:9001")
    s.send(msg)

    # result received from the service
    data = s.recv()

    s.close()
    return data
Это было полезно?

Решение

The problem is probably that you need to implement locking in send_to_master to ensure that only one pair socket is opened at a time across your application.

You could try implementing locking as detailed at: https://stackoverflow.com/a/10181810/288425

Here is a stab, though I haven't tested it:

from threading import Lock
lock = Lock()

def send_to_master(msg):
    context = zmq.Context()

    with lock:
        s = context.socket(zmq.PAIR)
        s.connect("tcp://localhost:9001")
        s.send(msg)

        # result received from the service
        data = s.recv()

        s.close()

    return data

...
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top