Skip to content

Use zero copy to send file if can#1288

Closed
abersheeran wants to merge 9 commits intoKludex:masterfrom
abersheeran:use-zerocopy
Closed

Use zero copy to send file if can#1288
abersheeran wants to merge 9 commits intoKludex:masterfrom
abersheeran:use-zerocopy

Conversation

@abersheeran
Copy link
Copy Markdown
Contributor

Comment thread starlette/responses.py Outdated
Comment thread starlette/responses.py Outdated
Comment thread starlette/responses.py
)
if self.send_header_only:
await send({"type": "http.response.body", "body": b"", "more_body": False})
elif "http.response.zerocopysend" in scope["extensions"]: # pragma: no cover
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should we not test it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how to test it. It seems too simple to just test if the result it returns is a dictionary. Do you have any suggestions?

@synodriver
Copy link
Copy Markdown

synodriver commented Oct 10, 2021

By the way, should we use range and if-range header? I saw it in someones toolkit

abersheeran and others added 2 commits October 10, 2021 23:00
Co-authored-by: Marcelo Trylesinski <[email protected]>
Co-authored-by: Marcelo Trylesinski <[email protected]>
@abersheeran
Copy link
Copy Markdown
Contributor Author

By the way, should we use range and if-range header? I saw it in someones toolkit

#1013

@synodriver
Copy link
Copy Markdown

By the way, should we use range and if-range header? I saw it in someones toolkit

#1013

Than you for pointing out. But is that means zerocopysend have to do something in order to implement that? Like adding offset and count params.

@abersheeran
Copy link
Copy Markdown
Contributor Author

In addition, baize.asgi.FileResponse can work with Starlette. Just try the following code

# main.py
from starlette.applications import Starlette
from starlette.routing import Route
from baize.asgi import FileResponse


async def homepage(request):
    return FileResponse("./main.py")

routes = [
    Route("/", endpoint=homepage)
]

app = Starlette(debug=True, routes=routes)

@abersheeran
Copy link
Copy Markdown
Contributor Author

By the way, should we use range and if-range header? I saw it in someones toolkit

#1013

Than you for pointing out. But is that means zerocopysend have to do something in order to implement that? Like adding offset and count params.

Yes. These parameters need to be specified.

@synodriver
Copy link
Copy Markdown

In addition, baize.asgi.FileResponse can work with Starlette. Just try the following code

# main.py
from starlette.applications import Starlette
from starlette.routing import Route
from baize.asgi import FileResponse


async def homepage(request):
    return FileResponse("./main.py")

routes = [
    Route("/", endpoint=homepage)
]

app = Starlette(debug=True, routes=routes)

Great feature!

@abersheeran
Copy link
Copy Markdown
Contributor Author

In addition, baize.asgi.FileResponse can work with Starlette. Just try the following code

# main.py
from starlette.applications import Starlette
from starlette.routing import Route
from baize.asgi import FileResponse


async def homepage(request):
    return FileResponse("./main.py")

routes = [
    Route("/", endpoint=homepage)
]

app = Starlette(debug=True, routes=routes)

Great feature!

Simply because they support the ASGI3 protocol. I was fascinated by this design in Starlette, so I implemented a similar part for WSGI.

@adriangb adriangb added feature New feature or request refactor Refactor code labels Feb 2, 2022
@Kludex Kludex removed the refactor Refactor code label Feb 2, 2022
Comment thread starlette/responses.py
}
)
finally:
await anyio.to_thread.run_sync(os.close, fd)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm... not comfortable with this.
Not at all obvious to me how the open/zerocopesend/close would interact.

I've got a bit of a preference here that we close this off for now as a "feature pause" until we're sufficiently on top of all existing issues that we can thoroughly review this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants