uasync slow

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
jm54
Posts: 6
Joined: Wed Jul 24, 2019 6:37 am

uasync slow

Post by jm54 » Fri Nov 22, 2019 8:30 pm

I wrote simple uasync tcp to serial bridge
but it is very slow .us2n at https://github.com/tiagocoutinho/us2n/b ... er/us2n.py is faster
someone could help me to improve my soft ? ( i am beginner )

Code: Select all

import uasyncio as asyncio

import machine, ssd1306
from machine import UART

import logging

# Init Display
i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21))
oled = ssd1306.SSD1306_I2C(128, 64, i2c, 0x3c)

#Init Uart
uart = UART(2)    # init with given baudrate
uart.init(115200, bits=8, parity=None, stop=1)

oled.fill(0)
oled.text("Boot", 0, 0)
oled.show()

async def server(reader, writer):
    print(reader, writer)
    sreader = asyncio.StreamReader(uart)
    swriter = asyncio.StreamWriter(uart, {})
    loop.create_task(ListenToSerial(writer,sreader))    
    loop.create_task(ListenToTcp(reader,swriter))

async def ListenToSerial(Wr,Sr):
     while True:
         FromSerial = await Sr.read(1)
         await Wr.awrite(FromSerial)
  
         
async def ListenToTcp(Re,Ws):    
        while True:            
            FromTcp=await Re.read(1)
            await Ws.awrite(FromTcp)
            

logging.basicConfig(level=None)
loop = asyncio.get_event_loop()
loop.call_soon(asyncio.start_server(server, "192.168.1.49", 8081))
loop.run_forever()

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: uasync slow

Post by pythoncoder » Sat Nov 23, 2019 11:23 am

You are reading characters one at a time. If your data is organised as lines of text, you might get faster processing using readline methods.

If data is not line-oriented you might read N characters at a time, but then you'd probably need a timeout to handle the case where fewer than N characters are sent to your bridge, followed by a long gap.
Peter Hinch
Index to my micropython libraries.

jm54
Posts: 6
Joined: Wed Jul 24, 2019 6:37 am

Re: uasync slow

Post by jm54 » Sun Nov 24, 2019 6:02 pm

dear peter
In fact i need reading one by one because this bridge , bridge one esp32 to repl uart of another esp32 or ttgo
with the soft ''tibbo'' your can map local virtual com to tcp port , like this you can use soft like thonny or EsPy
to access the uart port ( repl ) of the second esp or ttgo to program, see boot etc

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: uasyncio slow

Post by pythoncoder » Mon Nov 25, 2019 11:26 am

A baudrate of 115K implies an interval of about 80μs between characters. On an ESP32, even under ideal conditions, uasyncio takes longer than this to switch between tasks. Handling one character at a time you're never going to achieve that kind of throughput. I'd be astonished if you achieved 1/10 of that.

This is pushing uasyncio beyond what it can achieve. Your choices are either to handle data in larger quantities, or to abandon uasyncio in favour of a hard interrupt driven solution.
Peter Hinch
Index to my micropython libraries.

jm54
Posts: 6
Joined: Wed Jul 24, 2019 6:37 am

Re: uasync slow

Post by jm54 » Tue Nov 26, 2019 8:13 am

i made change reading 50 characters

async def server(reader, writer):
print(reader, writer)
sreader = asyncio.StreamReader(uart)
swriter = asyncio.StreamWriter(uart, {})
loop.create_task(ListenToSerial(writer,sreader))
loop.create_task(ListenToTcp(reader,swriter))

async def ListenToSerial(Wr,Sr):
while True:
FromSerial = await Sr.read(50)
print(FromSerial)
await Wr.awrite(FromSerial)

async def ListenToTcp(Re,Ws):
while True:
FromTcp=await Re.read(50)
await Ws.awrite(FromTcp)

it's quite good with thonny python
can I cancel task server when the communication ( tcp <> Serial ) is establish ? what is the best way ?

i use i2c display with init like this
i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21))
how to use it with asyncio ? like this?

async def display():
global count
while True:
count += 1
oled.fill(0)
oled.text('My string)', 0, 0)
oled.show()
await asyncio.sleep_ms(1000) # Pause 1s

Post Reply