ソースを参照

script/dnet: get async queue working

lunar-mining 2 年 前
コミット
39d27645b2
1 ファイル変更22 行追加4 行削除
  1. 22 4
      script/dnet/main.py

+ 22 - 4
script/dnet/main.py

@@ -43,7 +43,11 @@ class Dnetview:
                 pass
                 pass
     
     
         data = await rpc._make_request("p2p.get_info", [])
         data = await rpc._make_request("p2p.get_info", [])
-        logging.debug(f"get_info: {data}")
+
+        try:
+            self.queue.put_nowait(data)
+        except:
+            logging.debug("subscribe().put_nowait(): QueueFull")
 
 
         await rpc.dnet_switch(True)
         await rpc.dnet_switch(True)
         await rpc.dnet_subscribe_events()
         await rpc.dnet_subscribe_events()
@@ -51,8 +55,12 @@ class Dnetview:
         while True:
         while True:
             data = await rpc.reader.readline()
             data = await rpc.reader.readline()
             data = json.loads(data)
             data = json.loads(data)
-            # TODO: update data structures
-            logging.debug(f"events: {data}")
+            #logging.debug(f"events: {data}")
+
+            try:
+                self.queue.put_nowait(data)
+            except:
+                logging.debug("subscribe().putnowait(): QueueFull")
     
     
         await rpc.dnet_switch(False)
         await rpc.dnet_switch(False)
         await rpc.stop()
         await rpc.stop()
@@ -67,7 +75,17 @@ class Dnetview:
         async with asyncio.TaskGroup() as tg:
         async with asyncio.TaskGroup() as tg:
             for i, node in enumerate(nodes):
             for i, node in enumerate(nodes):
                 rpc = JsonRpc()
                 rpc = JsonRpc()
-                task = tg.create_task(self.subscribe(rpc, node['name'], node['port']))
+                subscribe = tg.create_task(self.subscribe(rpc, node['name'], node['port']))
+                update = tg.create_task(self.update_model())
+
+    async def update_model(self):
+        while True:
+            try:
+                item = await self.queue.get()
+                logging.debug(f"result: {item}")
+                self.queue.task_done()
+            except self.queue.is_empty():
+                logging.debug("update_model(): QueueEmpty")
 
 
     def main(self):
     def main(self):
         logging.basicConfig(filename='dnet.log', encoding='utf-8', level=logging.DEBUG)
         logging.basicConfig(filename='dnet.log', encoding='utf-8', level=logging.DEBUG)