I am trying to use the event flow provided by Kubernetes . api using the requests module. I came across what looks like a buffering problem: the requests module seems to be lagging behind a single event.
I have some code that looks something like this:
r = requests.get('http://localhost:8080/api/v1beta1/watch/services', stream=True) for line in r.iter_lines(): print 'LINE:', line
Since Kubernetes emits event notifications, this code will only display the last event that is issued when a new event arrives, which makes it almost completely useless for code that should respond to add / remove events.
I solved this by spawning curl in a subprocess instead of using the requests library:
p = subprocess.Popen(['curl', '-sfN', 'http://localhost:8080/api/watch/services'], stdout=subprocess.PIPE, bufsize=1) for line in iter(p.stdout.readline, b''): print 'LINE:', line
This works, but at the cost of some flexibility. Is there a way to avoid this buffering issue using the requests library?
python stream python-requests kubernetes
larsks
source share