I understand how to stream data over HTTP with requests:
import requests
r = requests.get(url, stream=True)
for line in r.iter_lines():
print(line)
I understand how to process a request asynchronously:
from requests_futures.sessions import FuturesSession
session = FuturesSession()
def bg_cb(sess, resp):
# parse the json storing the result on the response object
resp.data = resp.json()
future = session.get(url, background_callback=bg_cb)
# do some other stuff, send some more requests while this one works
response = future.result()
But I don't understand how to combine the two, that is, how to get callback to my designated function whenever the next line is available from the r.iter_lines()
:
session = FuturesSession()
def bg_cb(sess, resp):
# parse the json storing the result on the response object
resp.data = resp.json()
future = session.get(url, background_callback=bg_cb, stream=True)
# do other stuff here
The code snippet calls my bg_cb
function just once, before any data is actually available. Instead, I want bg_cb
to be called whenever a new line is available.