/ src / network / networkthread.py
networkthread.py
 1  """
 2  A thread to handle network concerns
 3  """
 4  import network.asyncore_pollchoose as asyncore
 5  from . import connectionpool
 6  from queues import excQueue
 7  from .threads import StoppableThread
 8  
 9  
10  class BMNetworkThread(StoppableThread):
11      """Main network thread"""
12      name = "Asyncore"
13  
14      def run(self):
15          try:
16              while not self._stopped:
17                  connectionpool.pool.loop()
18          except Exception as e:
19              excQueue.put((self.name, e))
20              raise
21  
22      def stopThread(self):
23          super(BMNetworkThread, self).stopThread()
24          for i in list(connectionpool.pool.listeningSockets.values()):
25              try:
26                  i.close()
27              except:  # nosec B110 # pylint:disable=bare-except
28                  pass
29          for i in list(connectionpool.pool.outboundConnections.values()):
30              try:
31                  i.close()
32              except:  # nosec B110 # pylint:disable=bare-except
33                  pass
34          for i in list(connectionpool.pool.inboundConnections.values()):
35              try:
36                  i.close()
37              except:  # nosec B110 # pylint:disable=bare-except
38                  pass
39  
40          # just in case
41          asyncore.close_all()