gevent


Why does this http streaming not work as expected?


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.

Related Links

building gevent in raspberry pi qemu
Some confusion about gevent (cffi) setup
Do rdflib and gevent work nicely together?
Replace gevent.spawn_link_exception
thread exception occurred in gevent
Is join required on gevent gthreads?
ZeroRPC heartbeats and timeouts
NewRelic doesn't graph any data in a Python + Gevent based system
Is there any way to run gevent-socketio 0.3.5-rc2 with gunicorn 18.0 without downgrading
How can I test to see whether gevent monkey patching is active?
How does gevent interact with threading.local data?
werkzeug server failed to handle request
Running time puzzle about gevent
Why does this http streaming not work as expected?

Categories

HOME
logging
excel-formula
redux
azure-active-directory
marie
display
crash
yuv
procmon
install
jquery-select2
browserstack
vsixmanifest
software-collections
minimax
velocity
aspectj
mongodb-replica-set
saucelabs
eclipse-orion
lambda-calculus
console-application
pylint
gpo
android-navigation-drawer
lotusscript
aws-iot
continuous-fourier
launchd
pypy
folder
arp
favorites
webclient
azure-storage-files
abide
yelp
gem-fury
ninja-forms
fisheye
sknode
susy
android-graphview
directadmin
compiler-warnings
rexx
superclass
denodo
spring-cloud-bus
beanshell
visio-2010
multi-select
zedgraph
apache-commons-cli
racket-student-languages
x12
xcode7.1
kik
archiva
radar-chart
android-progressbar
tooltwist
jeet-grid
cl.exe
cocos2d-x-2.x
switchpreference
image-preloader
xml-validation
tlbimp
oc4j
friendly-url
wpd
backbone-views
outlook-object-model
gwt2
system.transactions
blotter
gpl
tasklist
inflate
android-memory
event-propagation
beep
bindinglist
commonsware
fileoutputstream
scsf
maven-eclipse-plugin
openwrap
norton
conditional-operator
file-storage
atmega16
projectgen
ffdshow
moores-law
scalable

Resources

Encrypt Message