Message415124
Currently, _SelectorSocketTransport transport creates a copy of the data before sending which in case of large amount of data, can create multiple giga bytes copies of data before sending.
Script demonstrating current behavior:
-------------------------------------------------------------------------
import asyncio
import memory_profiler
@memory_profiler.profile
async def handle_echo(reader: asyncio.StreamReader, writer: asyncio.StreamWriter):
data = b'x' * 1024 * 1024 * 1000 # 1000 MiB payload
writer.write(data)
await writer.drain()
writer.close()
async def main():
server = await asyncio.start_server(
handle_echo, '127.0.0.1', 8888)
addrs = ', '.join(str(sock.getsockname()) for sock in server.sockets)
print(f'Serving on {addrs}')
async with server:
asyncio.create_task(server.start_serving())
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
while True:
data = await reader.read(1024 * 1024 * 100)
if not data:
break
asyncio.run(main())
-------------------------------------------------------------------------
Memory profile result:
------------------------------------------------------------------------
Filename: test.py
Line # Mem usage Increment Occurrences Line Contents
=============================================================
4 17.7 MiB 17.7 MiB 1 @memory_profiler.profile
5 async def handle_echo(reader: asyncio.StreamReader, writer: asyncio.StreamWriter):
6 1017.8 MiB 1000.1 MiB 1 data = b'x' * 1024 * 1024 * 1000 # 1000 MiB payload
7 2015.3 MiB 997.5 MiB 1 writer.write(data)
8 2015.3 MiB -988.1 MiB 2 await writer.drain()
9 1027.1 MiB -988.1 MiB 1 writer.close()
------------------------------------------------------------------------
To make it zero copy, python's buffer protocol can be used and use memory views of data to save RAM. The writelines method currently joins all the data before sending whereas it can use `socket.sendmsg` to make it more memory efficient.
Links:
- writelines - https://github.com/python/cpython/blob/2153daf0a02a598ed5df93f2f224c1ab2a2cca0d/Lib/asyncio/transports.py#L116
- socket.sendmsg - https://docs.python.org/3/library/socket.html#socket.socket.sendmsg
- memory_profiler -
https://pypi.org/project/memory-profiler/ |
|
Date |
User |
Action |
Args |
2022-03-14 09:18:42 | kumaraditya | set | recipients:
+ kumaraditya, gvanrossum, asvetlov, yselivanov |
2022-03-14 09:18:42 | kumaraditya | set | messageid: <1647249522.0.0.992096149336.issue47010@roundup.psfhosted.org> |
2022-03-14 09:18:41 | kumaraditya | link | issue47010 messages |
2022-03-14 09:18:41 | kumaraditya | create | |
|