sanic/docs/sanic/streaming.md

94 lines
2.5 KiB
Markdown
Raw Normal View History

2017-02-21 16:05:06 +00:00
# Streaming
Sanic allows you to stream content to the client with the `stream` method. This method accepts a coroutine callback which is passed a `StreamingHTTPResponse` object that is written to. A simple example is like follows:
```python
2017-02-21 16:38:57 +00:00
from sanic import Sanic
from sanic.response import stream
2017-02-21 16:05:06 +00:00
app = Sanic(__name__)
@app.route("/")
async def test(request):
async def sample_streaming_fn(response):
response.write('foo,')
response.write('bar')
2017-02-21 16:05:06 +00:00
return stream(sample_streaming_fn, content_type='text/csv')
```
This is useful in situations where you want to stream content to the client that originates in an external service, like a database. For example, you can stream database records to the client with the asynchronous cursor that `asyncpg` provides:
```python
@app.route("/")
async def index(request):
async def stream_from_db(response):
conn = await asyncpg.connect(database='test')
async with conn.transaction():
async for record in conn.cursor('SELECT generate_series(0, 10)'):
response.write(record[0])
2017-02-21 16:05:06 +00:00
return stream(stream_from_db)
2017-05-05 12:09:32 +01:00
```
## Request Streaming
Sanic allows you to get request data by stream, as below. When the request ends, `request.stream.get()` returns `None`.
```
from sanic import Sanic
2017-05-07 10:33:47 +01:00
from sanic.views import CompositionView
2017-05-05 12:09:32 +01:00
from sanic.blueprints import Blueprint
from sanic.response import stream, text
bp = Blueprint('blueprint_request_stream')
app = Sanic('request_stream', is_request_stream=True)
@app.stream('/stream')
async def handler(request):
async def streaming(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(streaming)
@app.get('/get')
async def get(request):
return text('OK')
@bp.stream('/bp_stream')
async def bp_handler(request):
result = ''
while True:
body = await request.stream.get()
if body is None:
break
result += body.decode('utf-8').replace('1', 'A')
return text(result)
2017-05-07 10:33:47 +01:00
async def post_handler(request):
result = ''
while True:
body = await request.stream.get()
if body is None:
break
result += body.decode('utf-8')
return text(result)
2017-05-05 12:09:32 +01:00
app.blueprint(bp)
2017-05-07 10:33:47 +01:00
view = CompositionView()
view.add(['POST'], post_handler, stream=True)
app.add_route(view, '/composition_view')
2017-05-05 12:09:32 +01:00
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8000)
```