From 6279eac3d19add390a3b0e21f2aa1e404d3a82c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=2E=20K=C3=A4rkk=C3=A4inen?= Date: Fri, 21 Feb 2020 10:51:58 +0200 Subject: [PATCH] Streaming request by async for. --- sanic/request.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/sanic/request.py b/sanic/request.py index 246eb351..4087dc33 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -56,6 +56,13 @@ class StreamBuffer: self._queue.task_done() return payload + async def __aiter__(self): + while True: + data = await self.read() + if not data: + return + yield data + async def put(self, payload): await self._queue.put(payload) @@ -161,6 +168,10 @@ class Request: def body_finish(self): self.body = b"".join(self.body) + async def receive_body(self): + assert self.body == [] + self.body = b"".join([data async for data in self.stream]) + @property def json(self): if self.parsed_json is None: