Sfoglia il codice sorgente

dnet: fix weird queue behavior by adding a sleep

¯\_(ツ)_/¯
lunar-mining 2 anni fa
parent
commit
618d3911d3
3 ha cambiato i file con 28 aggiunte e 27 eliminazioni
  1. 13 19
      bin/dnet/main.py
  2. 14 7
      bin/dnet/model.py
  3. 1 1
      bin/dnet/view.py

+ 13 - 19
bin/dnet/main.py

@@ -56,16 +56,13 @@ class Dnetview:
         await rpc.dnet_subscribe_events()
 
         while True:
+            await asyncio.sleep(0.01)
             data = await rpc.reader.readline()
             data = json.loads(data)
+
             info[name] = data
-            #logging.debug(f"{info}")
+            self.queue.put_nowait(info)
 
-            try:
-                self.queue.put_nowait(info)
-            except:
-                logging.debug("subscribe().putnowait(): QueueFull")
-    
         await rpc.dnet_switch(False)
         await rpc.stop()
     
@@ -86,19 +83,16 @@ class Dnetview:
 
     async def update_info(self):
         while True:
-            try:
-                info = await self.queue.get()
-                values = list(info.values())[0]
-                method = values.get("method")
-
-                if method == "dnet.subscribe_events":
-                    self.model.handle_event(info)
-                else:
-                    self.model.handle_nodes(info)
-
-                self.queue.task_done()
-            except OSError as e:
-                logging.debug("update_model(): error {}", e)
+            info = await self.queue.get()
+            values = list(info.values())[0]
+            method = values.get("method")
+
+            if method == "dnet.subscribe_events":
+                self.model.handle_event(info)
+            else:
+                self.model.handle_nodes(info)
+
+            self.queue.task_done()
 
     def main(self):
         logging.basicConfig(filename='dnet.log',

+ 14 - 7
bin/dnet/model.py

@@ -39,7 +39,6 @@ class Model:
 
     def handle_nodes(self, node):
         #logging.debug(f"p2p_get_info(): {node}")
-        #channel_lookup = {}
         name = list(node.keys())[0]
         values = list(node.values())[0]
         info = values["result"]
@@ -81,7 +80,6 @@ class Model:
         self.update_node(name, self.info)
 
     def handle_event(self, event):
-        #logging.debug(f"dnet_subscribe(): {event}")
         name = list(event.keys())[0]
         values = list(event.values())[0]
         params = values.get("params")
@@ -113,36 +111,46 @@ class Model:
             case "inbound_connected":
                 addr = info["addr"]
                 id = info.get("channel_id")
-                logging.debug(f"{name} inbound (connect): {addr}")
-                logging.debug(params)
                 self.info.update_inbound(f"{id}", addr)
+
+                logging.debug(f"{current_time}  inbound (connect):    {addr}")
             case "inbound_disconnected":
                 addr = info["addr"]
                 id = info.get("channel_id")
-                logging.debug(f"{name} inbound (disconnect): {addr}")
-                logging.debug(params)
                 self.info.remove_inbound(id)
+
+                logging.debug(f"{current_time}  inbound (disconnect): {addr}")
             case "outbound_slot_sleeping":
                 slot = info["slot"]
                 self.info.update_event((f"{name}", f"{slot}"), "sleeping")
+
+                logging.debug(f"{current_time}  slot {slot}: sleeping")
             case "outbound_slot_connecting":
                 slot = info["slot"]
                 addr = info["addr"]
                 self.info.update_event((f"{name}", f"{slot}"), f"connecting: addr={addr}")
+
+                logging.debug(f"{current_time}  slot {slot}: connecting   addr={addr}")
             case "outbound_slot_connected":
                 slot = info["slot"]
                 addr = info["addr"]
                 channel_id = info["channel_id"]
                 self.info.update_event((f"{name}", f"{slot}"), f"connected: addr={addr}")
+
+                logging.debug(f"{current_time}  slot {slot}: connected    addr={addr}")
             case "outbound_slot_disconnected":
                 slot = info["slot"]
                 err = info["err"]
                 self.info.update_event((f"{name}", f"{slot}"), f"disconnected: {err}")
+
+                logging.debug(f"{current_time}  slot {slot}: disconnected err='{err}'")
             case "outbound_peer_discovery":
                 attempt = info["attempt"]
                 state = info["state"]
                 self.info.update_event((f"{name}", "outbound"), f"peer discovery: {state} (attempt {attempt})")
 
+                logging.debug(f"{current_time}  peer_discovery: {state} (attempt {attempt})")
+
     def __repr__(self):
         return f"{self.nodes}"
     
@@ -162,7 +170,6 @@ class Info:
 
     def update_inbound(self, key, value):
         self.inbound[key] = value
-        logging.debug(self.inbound.items())
 
     def remove_inbound(self, key):
         if key in self.inbound:

+ 1 - 1
bin/dnet/view.py

@@ -188,7 +188,7 @@ class View():
                         for i, info in inbound.items():
                             widget = SlotView(node, i, info)
                             self.listwalker.contents.append(widget)
-
+                        #logging.debug(len(self.listwalker.contents))
                     if len(seed) != 0:
                         widget = ConnectView(node, "seed")
                         self.listwalker.contents.append(widget)