简体   繁体   中英

python flask: asynchronous requests

In a flask view I receive data via an API call and this data has to be passed to an external API that is sometimes very slow.

So I want the view to return a positive status code while the request is being handled async.

I have tried with requests-futures and have difficulties with the callback:

def bg_cb(sess, resp):
    print(resp.text)

@app.route('/incomingdata', methods=['POST',])
def clients():

    (... process incoming POST data and create outgoing API call. here I inserted a demo call to httpbin.org that simulates a very slow API ...)

    from requests_futures.sessions import FuturesSession
    session = FuturesSession()
    future = session.get('http://httpbin.org/delay/3', background_callback=bg_cb)
    response = future.result()

    return jsonify({'status': 'ok'}), 200

Unfortunately the above code will wait with the return until the callback has been processed. Here that is 3 seconds.

How can I achieve the result, that the view returns response with 200 immediately and after 3 seconds the callback function is called.

Thank you in advance!

You shouldn't use result() if you need just to call something asynchronously. The result() method will wait until background_callback will be finished. Just an example:

def bg_cb(sess, resp):
    print('done')

# in view:
print('call async...')
session.get('http://httpbin.org/delay/3', background_callback=bg_cb)

return jsonify({'status': 'ok'}), 200

Call endpoint, you will see response without delay. Console output:

call async...
# after few seconds
done

Now let's wait until callback will be finished:

future = session.get('http://httpbin.org/delay/3', background_callback=bg_cb)
print('wait result...')
response = future.result()
print('after result...')

Call endpoint, you will see response with delay. Console output:

wait result...
done
after result...

So, you don't need result() if you need just run something asynchronously.

Also you can use rq or celery if you need specific asynchronous processing.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM