I am trying to pass Server-Sent events from my Pyramid application, but I cannot figure out how to pass the response body from my view. Here's the test representation I'm using (it doesn't fully implement SSE, it's just meant to handle the streaming part):
@view_config(route_name='iter_test') def iter_test(request): import time def test_iter(): i = 0 while True: i += 1 if i == 5: raise StopIteration yield str(time.time()) print time.time() time.sleep(1) return test_iter()
This gives a ValueError: Could not convert return value of the view callable function pdiff.views.iter_test into a response object. The value returned was <generator object test_iter at 0x3dc19b0>. ValueError: Could not convert return value of the view callable function pdiff.views.iter_test into a response object. The value returned was <generator object test_iter at 0x3dc19b0>.
I tried return Response(app_iter=test_iter()) instead, which at least does not throw an error, but does not send a response - it waits for the generator to complete before returning the response to my browser.
I understand that it can simply return one event for each request and allow clients to reconnect after each event, but I would prefer to keep the real nature of the server-related events by streaming multiple events from one request without delaying reconnection. How can I do this with Pyramid?
python ajax pyramid server-sent-events
spiffytech
source share