Skip to content

Commit e76bd8a

Browse files
Tolmachofofasvetlov
authored andcommitted
FileResponse open/close files asynchronously (#3711)
1 parent a63e5e3 commit e76bd8a

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

aiohttp/web_fileresponse.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,11 @@ async def prepare(
337337
self.headers[hdrs.CONTENT_RANGE] = 'bytes {0}-{1}/{2}'.format(
338338
real_start, real_start + count - 1, file_size)
339339

340-
with (await loop.run_in_executor(None, filepath.open, 'rb')) as fobj:
341-
if start: # be aware that start could be None or int=0 here.
342-
await loop.run_in_executor(None, fobj.seek, start)
340+
fobj = await loop.run_in_executor(None, filepath.open, 'rb')
341+
if start: # be aware that start could be None or int=0 here.
342+
await loop.run_in_executor(None, fobj.seek, start)
343343

344+
try:
344345
return await self._sendfile(request, fobj, count)
346+
finally:
347+
await loop.run_in_executor(None, fobj.close)

0 commit comments

Comments
 (0)