S
S
Shlorpian2018-11-11 22:55:06
Python
Shlorpian, 2018-11-11 22:55:06

Flask, how to call a method and without waiting for its response return the response in jsonify?

There is a route, upon request of which the draw () method should be called, which in turn draws a picture using the Pillow library.
The problem is that all this rendering takes about 10 seconds (features of the e-ink display driver)

from draw import Drawing

@app.route(api_url('/<string:route>'), methods=['POST'])
def router(route):
    try:
        req_data = request.json
        if route == 'weather':
            Drawing().draw()
            return jsonify(status='Draw'), 200
        else:
            return abort(404)
    except Exception as e:
        logError(e)
        return abort(400)

The question is how to call the method so that rendering starts and at the same time return the request response in json without waiting for the response of this method ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2018-11-11
@VjokerA

It is necessary to separate the flies from the cutlets.
In the route, raise the "redraw needed" flag. As a separate thread (and even better - as a separate service), monitor this flag and react accordingly. Otherwise, you will run into problems as soon as more than 1 client accesses the route.

Z
Zanak, 2018-11-12
@Zanak

Async based on radish queues not an option? I'm talking about something like this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question