Streaming a generator

Hi there, I’m trying to do an endpoint that gets some files from a shared storage (in my case it is Minio), zips them and return the zip file.
To avoid loading all that data in memory or writing it to the FS I’m using generators to read chunks of data.
The service is done but I can’t find a way to use the sanic streaming with that generator since mixing async and generators doesn’t seem go work.

Is there any way I can stream a generator or will I have to either switch everything to async generators or write the zip to a file?

Thanks in advance

For anyone interested, I managed to stream my generator. I was having problems with the http response being cut off before the stream ended. I had to get the generator outside of the async function passed to the stream method and add a check to avoid sending empty chunks.

from sanic.response import stream


@app.route("/zip")
async def get_zip(request):
    files_zip_stream = document_service.get_files_zip()

    async def write_generator(response):
        for chunk in files_zip_stream:
            if len(chunk) > 0:  # Skip empty bytes to avoid ending http chunking early
                await response.write(chunk)
    return stream(write_generator, content_type='application/zip', chunked=True)

You can also do it in the new style:

@app.route("/zip")
async def get_zip(request):
    files_zip_stream = document_service.get_files_zip()
    response = await request.respond(content_type='application/zip',)

    for chunk in files_zip_stream:
        # You should not need to filter our 0 sized chunks
        await response.send(chunk)
    await response.eof()
    return response