30e6a310f1
* This commit adds handlers for the asyncio/uvloop protocol callbacks for pause_writing and resume_writing. These are needed for the correct functioning of built-in tcp flow-control provided by uvloop and asyncio. This is somewhat of a breaking change, because the `write` function in user streaming callbacks now must be `await`ed. This is necessary because it is possible now that the http protocol may be paused, and any calls to write may need to wait on an async event to be called to become unpaused. Updated examples and tests to reflect this change. This change does not apply to websocket connections. A change to websocket connections may be required to match this change. * Fix a couple of PEP8 errors caused by previous rebase. * update docs add await syntax to response.write in response-streaming docs. * remove commented out code from a test file
66 lines
1.6 KiB
Python
66 lines
1.6 KiB
Python
from sanic import Sanic
|
|
from sanic.views import CompositionView
|
|
from sanic.views import HTTPMethodView
|
|
from sanic.views import stream as stream_decorator
|
|
from sanic.blueprints import Blueprint
|
|
from sanic.response import stream, text
|
|
|
|
bp = Blueprint('blueprint_request_stream')
|
|
app = Sanic('request_stream')
|
|
|
|
|
|
class SimpleView(HTTPMethodView):
|
|
|
|
@stream_decorator
|
|
async def post(self, request):
|
|
result = ''
|
|
while True:
|
|
body = await request.stream.get()
|
|
if body is None:
|
|
break
|
|
result += body.decode('utf-8')
|
|
return text(result)
|
|
|
|
|
|
@app.post('/stream', stream=True)
|
|
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')
|
|
await response.write(body)
|
|
return stream(streaming)
|
|
|
|
|
|
@bp.put('/bp_stream', stream=True)
|
|
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)
|
|
|
|
|
|
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)
|
|
|
|
app.blueprint(bp)
|
|
app.add_route(SimpleView.as_view(), '/method_view')
|
|
view = CompositionView()
|
|
view.add(['POST'], post_handler, stream=True)
|
|
app.add_route(view, '/composition_view')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', port=8000)
|