Hi guys,
I am testing the server code by Peter from here on my ESP32 dev board: micropython-async/v3/as_drivers/client_server/userver.py
If I change the way I read from the stream on line 34 by calling sreader.read() rather than readline(), then there is around 3x to 4x performance boost. I am attempting to read a small HTTP header from a client, so readline() is quite useful in parsing the header as each header is separated line by line but if I read the whole thing in one go, it is way faster. If I have to use the read() function, then I have to manage the buffer myself in Python.
Does anyone have any ideas what could be the reason and what is the best way to approach this ?
Many thanks
Performance issue with streams and uasyncio on ESP32
Re: Performance issue with streams and uasyncio on ESP32
I assume the readline version in the code below (taken from https://github.com/peterhinch/micropyth ... userver.py) will happily read the last line and then wait for another one, until the timeout is reached.
The additional overhead caused by readline() and a task switch from the await is negligible, I think.
The additional overhead caused by readline() and a task switch from the await is negligible, I think.
Code: Select all
try:
while True:
try:
res = await asyncio.wait_for(sreader.readline(), self.timeout)
except asyncio.TimeoutError:
res = b''
if res == b'':
raise OSError
print('Received {} from client {}'.format(ujson.loads(res.rstrip()), self.cid))
swriter.write(res)
await swriter.drain() # Echo back
except OSError:
pass
A few hours of debugging might save you from minutes of reading the documentation!
My repositories: https://github.com/karfas
My repositories: https://github.com/karfas
Re: Performance issue with streams and uasyncio on ESP32
That is what I expect behvaiour-wise but I still do not understand the large difference in performance. If I read the whole request in one go (if bytes are available) and process it myself line by line, it is much faster
Re: Performance issue with streams and uasyncio on ESP32
What is the absolute difference in performance you observe ?
And where do you measure ?
For the echo reply readline() might be a few milliseconds slower.
For interpretation of the whole request you send (several lines) the last readline() will wait for the specified timeout (as described above).
And where do you measure ?
For the echo reply readline() might be a few milliseconds slower.
For interpretation of the whole request you send (several lines) the last readline() will wait for the specified timeout (as described above).
A few hours of debugging might save you from minutes of reading the documentation!
My repositories: https://github.com/karfas
My repositories: https://github.com/karfas