Работает у меня в отдельном потоке вот такой кусок кода:
while self._running.is_set():
line = self._sock.readline()
self._message_queue.put(line)
_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
_sock.connect((host, port))
self._sock = _sock.makefile()
В документации сие описано следующим образом:
When Sockets Die
Probably the worst thing about using blocking sockets is what happens when the other side comes down hard (without doing a close). Your socket is likely to hang. SOCKSTREAM is a reliable protocol, and it will wait a long, long time before giving up on a connection. If you’re using threads, the entire thread is essentially dead. There’s not much you can do about it. As long as you aren’t doing something dumb, like holding a lock while doing a blocking read, the thread isn’t really consuming much in the way of resources. Do not try to kill the thread - part of the reason that threads are more efficient than processes is that they avoid the overhead associated with the automatic recycling of resources. In other words, if you do manage to kill the thread, your whole process is likely to be screwed up.
Вопрос: что делать-то? Как перехватить смерть удаленной стороны?