Skip to content

Commit

Permalink
3313 payload works with io asynchronously (#3478)
Browse files Browse the repository at this point in the history
* I/O based payload works with I/O asynchronously

* I/O based payload works with I/O asynchronously
  • Loading branch information
Tolmachofof authored and asvetlov committed Jan 3, 2019
1 parent 37722f8 commit 4c7bc9f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
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 @@ -294,13 +295,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 @@ -339,13 +345,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

0 comments on commit 4c7bc9f

Please sign in to comment.