Difference between asyncio stream and synchronous stream for json output to a client

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
srinivasa rao pedada
Posts: 6
Joined: Sun Sep 12, 2021 3:57 pm

Difference between asyncio stream and synchronous stream for json output to a client

Post by srinivasa rao pedada » Mon Sep 13, 2021 1:40 pm

Code: Select all

response = {
'error': 'invalid request',
'status': 'retry'
}
synchronous side:

Code: Select all

conn.send('HTTP/1.1 200 OK\n')
conn.send('Content-Type: application/json\n')
conn.send('Connection: close\n\n')
conn.sendall(ujson.dumps(response ))
asyncio side:

Code: Select all

swriter.write(ujson.dumps(response ))
await swriter.drain()
Client was to able to read json response in synchronous way but reading failed in asyncio, Where is the error?

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

Re: Difference between asyncio stream and synchronous stream for json output to a client

Post by pythoncoder » Mon Sep 13, 2021 4:59 pm

It's hard to debug asynchronous code without having the full context (and I'm not a web programmer). But there's nothing wrong with the two lines of code you've shown us ;)

Is it possible that other tasks are hogging CPU time causing gaps in communication with consequent timeouts by the receiver?
Peter Hinch
Index to my micropython libraries.

srinivasa rao pedada
Posts: 6
Joined: Sun Sep 12, 2021 3:57 pm

Re: Difference between asyncio stream and synchronous stream for json output to a client

Post by srinivasa rao pedada » Tue Sep 14, 2021 3:33 am

got the error:

Code: Select all

asyncio.wait_for(sreader.readline(), self.timeout)
------> changed to

Code: Select all

asyncio.wait_for(sreader.read(2048), self.timeout)
. Now client is recieving json output immediately after closing the socket. Like you said its a timing issue, thank you sir.

Post Reply