# Streaming response with HTTP handler

**URL:** <https://discourse.nameko.io/t/streaming-response-with-http-handler/219>\
**Category:** googlegroup\
**Created:** [November 5, 2017, 3:44pm UTC](https://discourse.nameko.io/t/streaming-response-with-http-handler/219 "2017-11-05T15:44:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wayne\_Russell](https://avatars.discourse-cdn.com/v4/letter/w/cc9497/32.png) [@Wayne\_Russell](https://discourse.nameko.io/u/Wayne_Russell)\
**Post date:** [November 5, 2017, 3:44pm UTC](https://discourse.nameko.io/t/streaming-response-with-http-handler/219/1 "2017-11-05T15:44:08Z")

</div>

Hi

I'd like to return a streaming response from a service. Below is a simple  
example returning a generator in a Werkzeug Response but the response is  
not returned until the generator completes.

I would think the issue is how the Nameko HTTP handler gathers the response  
but I can't see the cause. Has anyone been able to stream a response?

Thanks,  
Wayne

import time  
from nameko.web.handlers import http  
from werkzeug.wrappers import Response

class SimpleRestApi:

&nbsp;&nbsp;&nbsp;&nbsp;name = 'rest\_api'

&nbsp;&nbsp;&nbsp;&nbsp;@http('GET', '/get\_generator')  
&nbsp;&nbsp;&nbsp;&nbsp;def get\_generator(self, request, value):

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def test\_gen():  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;import time  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for index in range(5):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield r'{0}\n'.format(json.dumps({'value': index}))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time.sleep(2)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return Response(test\_gen())

---

<div class="post-metadata">

**Author:** ![mattbennett](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.nameko.io/mattbennett/32/13_2.png) [@mattbennett](https://discourse.nameko.io/u/mattbennett)\
**Post date:** [November 6, 2017, 8:11am UTC](https://discourse.nameko.io/t/streaming-response-with-http-handler/219/2 "2017-11-06T08:11:07Z")

</div>

Hi Wayne,

I think this is probably because Nameko uses the Eventlet WSGI server. I  
don't know whether that supports a streaming response. You might want to  
ask over at [https://github.com/eventlet/eventlet/issues\](https://github.com/eventlet/eventlet/issues%5C). It's possible that  
is \*does\* support a streaming response and we've just not got it set up  
correctly in Nameko.

Matt.

> **···**
>
> On Sunday, November 5, 2017 at 3:44:08 PM UTC, Wayne Russell wrote:
> 
> > Hi
> > 
> > I'd like to return a streaming response from a service. Below is a simple  
> > example returning a generator in a Werkzeug Response but the response is  
> > not returned until the generator completes.
> > 
> > I would think the issue is how the Nameko HTTP handler gathers the  
> > response but I can't see the cause. Has anyone been able to stream a  
> > response?
> > 
> > Thanks,  
> > Wayne
> > 
> > import time  
> > from nameko.web.handlers import http  
> > from werkzeug.wrappers import Response
> > 
> > class SimpleRestApi:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;name = 'rest\_api'
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;@http('GET', '/get\_generator')  
> > &nbsp;&nbsp;&nbsp;&nbsp;def get\_generator(self, request, value):
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def test\_gen():  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;import time  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for index in range(5):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield r'{0}\n'.format(json.dumps({'value': index}))  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time.sleep(2)
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return Response(test\_gen())

---

<div class="post-metadata">

**Author:** ![David\_Szotten](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.nameko.io/david_szotten/32/6_2.png) [@David\_Szotten](https://discourse.nameko.io/u/David_Szotten)\
**Post date:** [November 6, 2017, 9:12am UTC](https://discourse.nameko.io/t/streaming-response-with-http-handler/219/3 "2017-11-06T09:12:26Z")

</div>

Even if it does we need to be careful here, since once the response is  
returned, nameko starts shutting down dependencies (which in theory could  
be accessed from the generator)

D

> **···**
>
> On Monday, 6 November 2017 08:11:07 UTC, Matt Yule-Bennett wrote:
> 
> > Hi Wayne,
> > 
> > I think this is probably because Nameko uses the Eventlet WSGI server. I  
> > don't know whether that supports a streaming response. You might want to  
> > ask over at [https://github.com/eventlet/eventlet/issues\](https://github.com/eventlet/eventlet/issues%5C). It's possible  
> > that is \*does\* support a streaming response and we've just not got it set  
> > up correctly in Nameko.
> > 
> > Matt.
> > 
> > On Sunday, November 5, 2017 at 3:44:08 PM UTC, Wayne Russell wrote:
> > 
> > > Hi
> > > 
> > > I'd like to return a streaming response from a service. Below is a simple  
> > > example returning a generator in a Werkzeug Response but the response is  
> > > not returned until the generator completes.
> > > 
> > > I would think the issue is how the Nameko HTTP handler gathers the  
> > > response but I can't see the cause. Has anyone been able to stream a  
> > > response?
> > > 
> > > Thanks,  
> > > Wayne
> > > 
> > > import time  
> > > from nameko.web.handlers import http  
> > > from werkzeug.wrappers import Response
> > > 
> > > class SimpleRestApi:
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;name = 'rest\_api'
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;@http('GET', '/get\_generator')  
> > > &nbsp;&nbsp;&nbsp;&nbsp;def get\_generator(self, request, value):
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def test\_gen():  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;import time  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for index in range(5):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield r'{0}\n'.format(json.dumps({'value': index}))  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time.sleep(2)
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return Response(test\_gen())
