Add Request.stream

This commit is contained in:
38elements
2017-05-05 20:09:32 +09:00
parent 7cf3d49f00
commit 9f2ba26e9d
7 changed files with 116 additions and 8 deletions

View File

@@ -29,4 +29,31 @@ async def index(request):
response.write(record[0])
return stream(stream_from_db)
```
```
## Request Streaming
Sanic allows you to get request data by stream, as below. It is necessary to set `is_request_stream` to True. When the request ends, `request.stream.get()` returns `None`. It is not able to use common request and request stream in same application.
```
from sanic import Sanic
from sanic.response import stream
app = Sanic(__name__, is_request_stream=True)
@app.post('/stream')
async def handler(request):
async def sample_streaming_fn(response):
while True:
body = await request.stream.get()
if body is None:
break
body = body.decode('utf-8').replace('1', 'A')
response.write(body)
return stream(sample_streaming_fn)
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8000)
```