◐ Shell
clean mode source ↗

Speedup readexactly by socketpair · Pull Request #395 · python/asyncio

I have benchmarked that.
8.72 on master branch
6.65 on this branch.

program:

#!/usr/bin/env python3.5

import asyncio
import os
import time

async def action(rs, chunksize):
    try:
        while True:
            await rs.readexactly(chunksize)
    except asyncio.IncompleteReadError:
        pass

async def writer(ws, bufsize):
    ws.write(b'x' * bufsize)
    await ws.drain()
    ws.close()

async def amain():
    bufsize = 30 * 1024 * 1024
    chunksize = 16

    handler = lambda rs, ws: writer(ws, bufsize)
    server = await asyncio.start_server(handler, '127.0.0.1', 0)
    addr = server.sockets[0].getsockname()
    (rs, ws) = await asyncio.open_connection(*addr)
    server.close()
    await server.wait_closed()
    # now writing part is running "in background". TODO: wait it completion somehow

    await asyncio.sleep(3)

    a = time.monotonic()
    await action(rs, chunksize)
    b = time.monotonic()
    ws.close()
    print('chunk size={}: {:.2f} seconds.'.format(
        chunksize,
        b - a,
    ))


loop = asyncio.get_event_loop()
loop.run_until_complete(amain())