Slow asyncio.open_connection

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
ajvperth
Posts: 11
Joined: Fri Jul 08, 2022 12:21 am

Slow asyncio.open_connection

Post by ajvperth » Wed Jul 20, 2022 2:12 pm

I use an ESP32 to communicate over WiFi with a linux desktop.
If the ESP32 is configured as an asyncio echo server and the PC as a client the average go around time is 30 mSec
If the ESP32 is configured as an asyncio client contacting the PC running an echo server $ socat tcp-l:5000,fork exec:'bin/cat' the go around time is typical 430 mSec, 15 times slower than the other way around.
The reader, writer = await asyncio.open_connection(ip, port) requires 200 mSec while no other tasks are running.
Is there an explanation for this, can my code be improved?

Code: Select all

async def contactServer(ip, port, msg):
    try:
        reader, writer = await asyncio.open_connection(ip, port)
        writer.write(msg)
        await writer.drain()
        r = await reader.read(1024)
    except:
        r = None
    writer.close()
    await writer.wait_closed()
    return r
thanks

ajvperth
Posts: 11
Joined: Fri Jul 08, 2022 12:21 am

Re: Slow asyncio.open_connection

Post by ajvperth » Wed Jul 20, 2022 2:41 pm

The turn around time is improved from 430 down to 220 mSec by adding encode() and decode()

Code: Select all

async def contactServer(ip, port, msg):
    try:
        reader, writer = await asyncio.open_connection(ip, port)
        writer.write(msg.encode())
        await writer.drain()
        r = await reader.read(1024)
        r = r.decode()
    except:
        r = None
    writer.close()
    await writer.wait_closed()
    return r
but still 7 times slower then the other way around.
Now, I do not think it is asyncio related, as blocking sockets also require 400 mSec

Post Reply