Skip to content

Commit

Permalink
[PR #10059/aac6f741 backport][3.11] Combine executor jobs in FileResp…
Browse files Browse the repository at this point in the history
…onse sendfile_fallback (#10062)

Co-authored-by: J. Nick Koston <[email protected]>
  • Loading branch information
patchback[bot] and bdraco authored Nov 27, 2024
1 parent a5a6981 commit 1b78cae
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions aiohttp/web_fileresponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ def __init__(
self._path = pathlib.Path(path)
self._chunk_size = chunk_size

def _seek_and_read(self, fobj: IO[Any], offset: int, chunk_size: int) -> bytes:
fobj.seek(offset)
return fobj.read(chunk_size) # type: ignore[no-any-return]

async def _sendfile_fallback(
self, writer: AbstractStreamWriter, fobj: IO[Any], offset: int, count: int
) -> AbstractStreamWriter:
Expand All @@ -96,10 +100,9 @@ async def _sendfile_fallback(

chunk_size = self._chunk_size
loop = asyncio.get_event_loop()

await loop.run_in_executor(None, fobj.seek, offset)

chunk = await loop.run_in_executor(None, fobj.read, chunk_size)
chunk = await loop.run_in_executor(
None, self._seek_and_read, fobj, offset, chunk_size
)
while chunk:
await writer.write(chunk)
count = count - chunk_size
Expand Down

0 comments on commit 1b78cae

Please sign in to comment.