Communication delay problem using urequest. #16139
-
Hi! After adding some debugging logs, I came to the conclusion that this part of the code in urequest takes the most time: I was fairly successful in getting a response from the server with the data, but there was a problem when trying response.json(). I had to wait about 70 seconds before the device parsed the response into json.
In addition, I have also simplified the request method to suit only my needs, added debugging logs and I added some code to support a keep-alive connection as far as MicroPython is concerned:
And then I noticed in the REPL:
As you can see, the device waited 60-70 seconds to execute socket.read(4096). So something must have been blocking the socket and this caused such a long delay in processing the response. I would add that when I send the Connection: close header there is absolutely no problem with reading data from the socket. My question is what could be causing this socket to block and am I even able to handle keep-alive POST requests in MicroPython? And if not, is there any other way of communicating with the server so that it is practically instant (I would expect the communication speed to stabilise and go below 1000ms, and preferably as quick as possible). Thank you in advance for your feedback. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
shouldn't the |
Beta Was this translation helpful? Give feedback.
-
Ok, I figured out that socket.read() won't execute until the socket is closed - that's why about 70 seconds pass because eventually the server closes the connections and only then am I able to read data from the socket. Is there any way to read data from the socket while the connection is still open? According to the keep-alive logic, I doesn't want to close it. |
Beta Was this translation helpful? Give feedback.
-
I had to set socket.setblocking(False) before doing wrap_socket and then everything started working perfectly fine. I also had to make a loop when reading status line from socket until it was read correctly (sometimes trying to read was faster than anything appeared on the socket?) Thanks for your help! |
Beta Was this translation helpful? Give feedback.
I had to set socket.setblocking(False) before doing wrap_socket and then everything started working perfectly fine. I also had to make a loop when reading status line from socket until it was read correctly (sometimes trying to read was faster than anything appeared on the socket?) Thanks for your help!