Skip to content

Commit

Permalink
ENH: define connection timeout to prevent hanging and set to 1 second
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLP committed Jun 8, 2018
1 parent ac5164b commit e801965
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions basil/TL/SiTcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,23 @@ def init(self):
raise ValueError('Parameter \'ip\' missing.')
if 'udp_port' not in self._init:
raise ValueError('Parameter \'udp_port\' missing.')

connect_timeout = self._init.get('connect_timeout', 1)

self._sock_udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self._sock_udp.settimeout(connect_timeout)
self._sock_udp.connect((self._init['ip'], self._init['udp_port']))
self._sock_udp.settimeout(None) # https://stackoverflow.com/questions/3432102/python-socket-connection-timeout
# using select to monitor socket status, therefore the socket is set to blocking (default)
self._sock_udp.setblocking(0)
# start readout thread if TCP connection is set
if 'tcp_connection' in self._init and self._init['tcp_connection']:
if 'tcp_port' not in self._init:
raise ValueError('Parameter \'tcp_port\' missing.')
self._sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._sock_tcp.settimeout(connect_timeout)
self._sock_tcp.connect((self._init['ip'], self._init['tcp_port']))
self._sock_tcp.settimeout(None) # https://stackoverflow.com/questions/3432102/python-socket-connection-timeout
# using select to monitor socket status, therefore the socket is set to blocking (default)
self._sock_tcp.setblocking(0)
self._tcp_readout_thread = Thread(target=self._tcp_readout, name='TcpReadoutThread', kwargs={})
Expand Down

0 comments on commit e801965

Please sign in to comment.