I have a server that is trying to stream some content to a client (which is Kenneth Reitz excellent requests library) – (Props to toastdriven.com for code below). Note that in a browser, it works as expected.
from gevent import monkey
monkey.patch_all()
import datetime
import time
from gevent import Greenlet
from gevent import pywsgi
from gevent import queue
import json
def current_time(body):
current = start = datetime.datetime.now()
end = start + datetime.timedelta(seconds=60)
while current < end:
current = datetime.datetime.now()
message = json.dumps({'time': current.strftime("%Y-%m-%d %I:%M:%S")})
body.put(message)
time.sleep(1)
body.put('</body></html>')
body.put(StopIteration)
def handle(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
body = queue.Queue()
g = Greenlet.spawn(current_time, body)
return body
server = pywsgi.WSGIServer(('127.0.0.1', 1234), handle)
print "Serving on http://127.0.0.1:1234..."
server.serve_forever()
And a client:
import sys
import requests
import json
my_config = {'verbose': sys.stdout}
r = requests.get('http://127.0.0.1:1234/', config=my_config)
for line in r.iter_lines():
print json.loads(line)
I dont’ understand why the json lines arent’ showing up in the terminal (OSX). When I ctrl-c the responses are dumped to the screen.
If I do:
for line in r.iter_content()
I get the json, a character on each line, streamed as expected.
Any ideas?
Your client is probably running in a terminal window that is buffered. After each print, try adding
sys.stdout.flush()to flush the output buffer.