better shutdown

This commit is contained in:
j 2014-09-09 15:07:33 +02:00
parent 88a5ed9edc
commit 417d02bdd4

View file

@ -59,14 +59,12 @@ class LocalNodesBase(Thread):
def join(self): def join(self):
self._active = False self._active = False
'''
if self._socket: if self._socket:
try: try:
self._socket.shutdown(socket.SHUT_RDWR) self._socket.shutdown(socket.SHUT_RDWR)
except: except OSError:
pass pass
self._socket.close() self._socket.close()
'''
return Thread.join(self) return Thread.join(self)
def new_node(self, data): def new_node(self, data):
@ -80,7 +78,6 @@ class LocalNodesBase(Thread):
while self._active: while self._active:
try: try:
s = self.get_socket() s = self.get_socket()
s.settimeout(2)
s.bind(('', self._PORT)) s.bind(('', self._PORT))
while self._active: while self._active:
data, addr = s.recvfrom(1024) data, addr = s.recvfrom(1024)
@ -91,13 +88,15 @@ class LocalNodesBase(Thread):
if data: if data:
self.update_node(data) self.update_node(data)
except socket.timeout: except socket.timeout:
now = time.mktime(time.localtime()) if self._active:
if now - last > 60: now = time.mktime(time.localtime())
last = now if now - last > 60:
_thread.start_new_thread(self.send, ()) last = now
_thread.start_new_thread(self.send, ())
except: except:
logger.debug('receive failed. restart later', exc_info=1) if self._active:
time.sleep(10) logger.debug('receive failed. restart later', exc_info=1)
time.sleep(10)
def run(self): def run(self):
self.send() self.send()