Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3313 payload works with io asynchronously #3478

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/3313.feature
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
FileResponse from web_fileresponse.py uses a ThreadPoolExecutor to work with files asynchronously.
I/O based payloads from payload.py uses a ThreadPoolExecutor to work with I/O objects asynchronously.
23 changes: 17 additions & 6 deletions aiohttp/payload.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import enum
import io
import json
Expand Down Expand Up @@ -285,13 +286,18 @@ def __init__(self,
self.set_content_disposition(disposition, filename=self._filename)

async def write(self, writer: AbstractStreamWriter) -> None:
loop = asyncio.get_event_loop()
try:
chunk = self._value.read(DEFAULT_LIMIT)
chunk = await loop.run_in_executor(
None, self._value.read, DEFAULT_LIMIT
)
while chunk:
await writer.write(chunk)
chunk = self._value.read(DEFAULT_LIMIT)
chunk = await loop.run_in_executor(
None, self._value.read, DEFAULT_LIMIT
)
finally:
self._value.close()
await loop.run_in_executor(None, self._value.close)


class TextIOPayload(IOBasePayload):
Expand Down Expand Up @@ -330,13 +336,18 @@ def size(self) -> Optional[float]:
return None

async def write(self, writer: AbstractStreamWriter) -> None:
loop = asyncio.get_event_loop()
try:
chunk = self._value.read(DEFAULT_LIMIT)
chunk = await loop.run_in_executor(
None, self._value.read, DEFAULT_LIMIT
)
while chunk:
await writer.write(chunk.encode(self._encoding))
chunk = self._value.read(DEFAULT_LIMIT)
chunk = await loop.run_in_executor(
None, self._value.read, DEFAULT_LIMIT
)
finally:
self._value.close()
await loop.run_in_executor(None, self._value.close)


class BytesIOPayload(IOBasePayload):
Expand Down