Skip to content

[FeatureRequest] Add support for generator output #929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mockodin opened this issue Dec 16, 2021 · 5 comments
Closed

[FeatureRequest] Add support for generator output #929

mockodin opened this issue Dec 16, 2021 · 5 comments
Assignees
Milestone

Comments

@mockodin
Copy link

mockodin commented Dec 16, 2021

azure.functions.HttpResponse currently supports only str/bytes content (body). This has a high memory overhead for delivery of large returns. Adding support for a generator would be ideally to support larger returns without the high memory consumption. This is commonly supported on other Response objects such as flask.Response

import azure.functions as func
from pathlib import Path

def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
    def get_file(file):
        with io.open(file, 'rb') as file_hdl:
            chunk = file_hdl.read(1024)
            while chunk:
                yield chunk
                chunk = file_hdl.read(1024)
            if Path(file).exists():
                Path(file).unlink()
    for input_file in req.files.values():
        with tempfile.NamedTemporaryFile(mode='wb', suffix=Path(input_file.filename).suffix) as in_file:
            input_file.save(in_file)
            with tempfile.NamedTemporaryFile(mode='rb', suffix=Path(input_file.filename).suffix) as out_file:
                # manipulate file somehow
                return func.HttpResponse(body=get_file(out_file.name), mimetype='video/mp4', headers={"Content-Type":"video/mp4", "Content-Disposition":"inline", "Content-Transfer-Enconding":"binary", "Content-Length":Path(out_file.name).stat().st_size})

@v-bbalaiagar v-bbalaiagar self-assigned this Feb 17, 2022
@v-bbalaiagar
Copy link

Hi @mockodin , Thank you for your feedback! Adding this issue as a feature request to consider this in future implementations.

@sudoPete
Copy link

sudoPete commented Apr 1, 2022

Has there been any progress on this? I am trying to use Fast API and Starlette to return large files via an HTTP trigger on functions, but the lack of ability to use streaming severely limits the usefulness of functions for such a task.

@ligonliu
Copy link

I also want this functionality. Based on the current code structure, maybe the easiest way to implement this is to update downstream code handling func.HttpResponse to accept iterators that yield str/bytes.

For the OP's specific use case, there is an easier implementation that requires very small change of code: mmap the video file as read only, and pass the mmap object to func.HttpResponse which is a bytearray-like object.

@YunchuWang Can you please point me to the code of serving the response body to HTTP server? I might be able to submit a patch, beginning from:

`

class HttpResponse(_abc.HttpResponse):
......
def __set_body(self, body):
if isinstance(body, str):
body = body.encode(self.__charset)

    if not isinstance(body, (bytes, bytearray)):   ----> add type memoryview, memoryview object can be constructed from mmap object
        raise TypeError(
            f'response is expected to be either of '
            f'str, bytes, or bytearray, got {type(body).__name__}')

    self.__body = bytes(body)

`

@YunchuWang
Copy link
Member

YunchuWang commented Dec 14, 2023

sorry for late followup! thanks for all feedback. Recently function host started supporting http streaming and we have been working on supporting http streaming going to release early next year(jan-feb). Will post update once thats released, stay tuned!

@YunchuWang YunchuWang mentioned this issue Apr 11, 2024
5 tasks
@hallvictoria
Copy link
Contributor

Addressed by HTTP Streaming. Please see this documentation for more information:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants