Skip to content

Commit 1b78cae

Browse files
[PR #10059/aac6f741 backport][3.11] Combine executor jobs in FileResponse sendfile_fallback (#10062)
Co-authored-by: J. Nick Koston <[email protected]>
1 parent a5a6981 commit 1b78cae

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

aiohttp/web_fileresponse.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ def __init__(
8888
self._path = pathlib.Path(path)
8989
self._chunk_size = chunk_size
9090

91+
def _seek_and_read(self, fobj: IO[Any], offset: int, chunk_size: int) -> bytes:
92+
fobj.seek(offset)
93+
return fobj.read(chunk_size) # type: ignore[no-any-return]
94+
9195
async def _sendfile_fallback(
9296
self, writer: AbstractStreamWriter, fobj: IO[Any], offset: int, count: int
9397
) -> AbstractStreamWriter:
@@ -96,10 +100,9 @@ async def _sendfile_fallback(
96100

97101
chunk_size = self._chunk_size
98102
loop = asyncio.get_event_loop()
99-
100-
await loop.run_in_executor(None, fobj.seek, offset)
101-
102-
chunk = await loop.run_in_executor(None, fobj.read, chunk_size)
103+
chunk = await loop.run_in_executor(
104+
None, self._seek_and_read, fobj, offset, chunk_size
105+
)
103106
while chunk:
104107
await writer.write(chunk)
105108
count = count - chunk_size

0 commit comments

Comments
 (0)