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!