Streaming response with HTTP handler

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:

    name = 'rest_api'

    @http('GET', '/get_generator')
    def get_generator(self, request, value):

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

        return Response(test_gen())

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. 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:

    name = 'rest_api'

    @http('GET', '/get_generator')
    def get_generator(self, request, value):

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

        return Response(test_gen())

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. 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:

    name = 'rest_api'

    @http('GET', '/get_generator')
    def get_generator(self, request, value):

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

        return Response(test_gen())