paho.mqtt.python icon indicating copy to clipboard operation
paho.mqtt.python copied to clipboard

connection timeout

Open deiv23 opened this issue 6 years ago • 6 comments

Is there any way of setting a timeout for the client.connect method?

deiv23 avatar Jan 21 '19 12:01 deiv23

I want to do the same thing. I'm having a connection timeout error as well when I try to establish connection over TLS. I initially thought the networks was slow but OpenVPN works on the same network. How can I modify the timeout value when connecting?

agm30 avatar Mar 13 '19 06:03 agm30

Hello guys, from what I can understand here you can just set the timeout by setting the keepalive variable. Give it a shot and see what comes of it.

vrst37 avatar Mar 28 '19 09:03 vrst37

For me this doesn't work. The problem is that inside reconnect method there is a call to socket.create_connection. This function has a default the default timeout "_GLOBAL_DEFAULT_TIMEOUT". The solution for me is to modify socket.create_connection((self._host, self._port)) call with socket.create_connection((self._host, self._port), timeout=self._keepalive)

escrichov avatar May 08 '19 13:05 escrichov

This issue hasn't been resolved.

gilbertwong96 avatar Jun 18 '21 03:06 gilbertwong96

As a workaround you can set default socket timeout and then use client.connect.

import socket
socket.setdefaulttimeout(timeout)

azdobylak avatar Aug 13 '21 09:08 azdobylak

The default timeout for socket doesn't work, using paho.mqtt.publish.single as it probably get's overwritten by paho in the process.

creyD avatar Apr 14 '22 13:04 creyD

Changes have been made since this issue was raised; you can now use:

mqtt_client = mqtt.Client()
mqtt_client._connect_timeout = 1.0

I believe that this addresses the original issue so am going to close this. If you are still having problems then please raise a new issue (but please include as much information as possible including code/logs).

MattBrittan avatar Dec 23 '23 23:12 MattBrittan