Add stream decorator for HTTPMethodView

This commit is contained in:
38elements
2017-05-07 21:11:40 +09:00
parent 931397c7e1
commit 0b53c413a7
6 changed files with 67 additions and 3 deletions

View File

@@ -6,5 +6,5 @@ data = ""
for i in range(1, 250000):
data += str(i)
r = requests.post('http://127.0.0.1:8000/bp_stream', data=data)
r = requests.post('http://127.0.0.1:8000/method_view', data=data)
print(r.text)

View File

@@ -1,5 +1,7 @@
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
@@ -7,6 +9,19 @@ 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.stream('/stream')
async def handler(request):
async def streaming(response):
@@ -45,6 +60,7 @@ async def post_handler(request):
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')