socket.getaddrinfo takes too long to respond

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
akushva
Posts: 19
Joined: Fri May 03, 2019 10:08 am

socket.getaddrinfo takes too long to respond

Post by akushva » Mon Aug 12, 2019 6:08 am

I'm trying to use socket library in my application on ESP32. I'm trying to connect to a device via it's IP address using the command below-

sock.connect(socket.getaddrinfo(device_IP, device_port)[0][-1])

I noticed that it takes around 19-20 seconds whenever there is a failure in connection or I put wrong IP address, after which it responds with this error:

[Errno 113] EHOSTUNREACH

The timeout argument in socket library doesn't help it and I'm am unable to connect to a device when I configure the socket to be a non-blocking socket.
Is there a way so that I can reduce this waiting time down to 2-4 seconds.

Thanks.

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

Re: socket.getaddrinfo takes too long to respond

Post by pythoncoder » Tue Aug 13, 2019 8:19 am

Perhaps I'm missing something but I can't replicate this on the ESP32 reference board. The following code runs "instantly" whether or not a connection to my WiFi is running. The device does not exist on my network and a ping produces "Destination Host Unreachable".

Code: Select all

>>> usocket.getaddrinfo('192.168.0.55', 1234)[0][-1]
('192.168.0.55', 1234)
>>> 
You could create the tuple directly, but this is a hack and your approach is the correct one.

If you're doing a DNS lookup by name getaddrinfo will block for a period depending on the response time of your DNS server. This is a known limitation. The need for a nonblocking solution has been discussed but as far as I know has not been written.
Peter Hinch
Index to my micropython libraries.

Post Reply