Repository navigation
Replies: 4 comments
|
Hi @Temperosa! And thanks for this proposal. I am afraid this is a "wontfix" for now, although we can take a look at it and discuss. WSGIThe PEP-3333 spec explicitly require that the response iterable yield bytestrings. Even if the spec is somewhat vague in its wording, from wsgiref.validate import validator
from wsgiref.simple_server import make_server
def app(environ, start_response):
status = '200 OK'
headers = [('Content-Type', 'text/plain'), ('Content-Length', '12')]
start_response(status, headers)
return [memoryview(b"Hello World!")]
if __name__ == '__main__':
validated_app = validator(app)
server = make_server('127.0.0.1', 8000, validated_app)
server.handle_request() # Serve exactly one request, then exitRunning this, and sending a request to http://localhost:8000/, results in an HTTP 500 with a traceback on the Python side: ASGIThe spec says:
Falcon does accept So unfortunately I don't think this can be glossed over with typing changes, |
|
Thanks for response, @vytas7 ! Just to clarify the proposal, I was referencing I was looking for a way to achieve zero-copy responses, and |
|
A workaround could be to define a custom response type, although it still cannot avoid one typing exception. I called this import http
import granian
import falcon
import falcon.asgi
A_LOAD_OF_MEMORY = memoryview(b'123456789abcdef\n' * 32 * 1024 * 1024)
class ZeroCopyResponse(falcon.asgi.Response):
data: None | bytes | bytearray | memoryview # type: ignore[assignment]
class MiddlewareExample:
async def process_request(
self, req: falcon.asgi.Request, resp: ZeroCopyResponse) -> None:
if req.path == '/example':
resp.content_type = 'application/octet-stream'
resp.data = A_LOAD_OF_MEMORY[:32]
resp.complete = True
class MemoryView:
async def on_get(self, req: falcon.asgi.Request, resp: ZeroCopyResponse) -> None:
start, end = 0, len(A_LOAD_OF_MEMORY) - 1
if req.range is not None:
start, end = req.range
resp.content_range = (start, end, len(A_LOAD_OF_MEMORY))
resp.status = http.HTTPStatus.PARTIAL_CONTENT
resp.content_type = falcon.MEDIA_TEXT
resp.data = A_LOAD_OF_MEMORY[start: end+1]
app = falcon.asgi.App(response_type=ZeroCopyResponse)
app.add_middleware(MiddlewareExample())
app.add_route('/data', MemoryView())
if __name__ == '__main__':
server = granian.Granian(
target='memoryview:app', interface=granian.constants.Interfaces.ASGI)
server.serve()On CPython 3.13+, this passes type checking: $ mypy --strict memoryview.py
Success: no issues found in 1 source fileAnd it works "just fine": GET /data HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Host: localhost:8000
Range: bytes=0-31
User-Agent: xh/0.18.0
HTTP/1.1 206 Partial Content
Content-Length: 32
Content-Range: bytes 0-31/536870912
Content-Type: text/plain; charset=utf-8
Date: Thu, 10 Sep 2026 11:13:37 GMT
Server: granian
123456789abcdef
123456789abcdefThat "just fine" we can inadvertently break at any time... However, Falcon is in general very careful about the impact of any changes, supported or not. Now that we know about this usecase, we'll probably have it in mind too 😅 |
|
I'm going to convert this issue to a discussion since it's not really much we can do here, even though zero-copy response from shared memory would really align with Falcon's philosophy. If an ASGI extension pops up for this, we can consider implementing it. At the time of this writing, both WSGI and ASGI only support |
Uh oh!
There was an error while loading. Please reload this page.
The current version of Response.data setter is typed as
def data(self, value: bytes | None) -> None:and every type checker is complaining when doingresponse.data = memoryview(b""), while the response is successfully sent.I propose updating the type annotation for Response.data from
bytes | NonetoBuffer | None(usingcollections.abc.Buffer). This change allows passingmemoryviewobjects directly without type checkers errors and spare the user from ignore comments or type casting.All reactions