azure-functions-python-worker
azure-functions-python-worker copied to clipboard
[FeatureRequest] Add support for generator output
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})
Hi @mockodin , Thank you for your feedback! Adding this issue as a feature request to consider this in future implementations.
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.