Browse Source

异步下载资源

zhangyh 9 months ago
parent
commit
b8217556ea
3 changed files with 49 additions and 60 deletions
  1. 1 6
      python/index.py
  2. 47 53
      python/service/init_load_source.py
  3. 1 1
      python/utils/utils_func.py

+ 1 - 6
python/index.py

@@ -32,12 +32,7 @@ async def run_server():
     await server.serve()
 
 async def main():
-    # 创建后台任务
-    task = asyncio.create_task(init_load_source().load_source())
-    # 启动服务器
-    print(">>>>>>>>>>>>>>>>>>>>>>>>>>")
-    await run_server()
-
+    await  asyncio.gather(run_server(), init_load_source().load_source())
 
 
 if __name__ == "__main__":

+ 47 - 53
python/service/init_load_source.py

@@ -2,84 +2,78 @@ import asyncio
 import json
 import time
 from settings import HLM_HOST
-import requests
+import aiohttp
 import os
-from utils.utils_func import get_md5,get_modified_time,compare_two_times,download_file
-
+from utils.utils_func import get_md5, get_modified_time, compare_two_times, check_path
 
 class init_load_source:
 
-    def __init__(self):
-        pass
-
     async def load_source(self):
-        # asyncio.sleep()
-        # await self.down_resouce()
-
-        for i in range(100):
-            print(1)
-            await  asyncio.sleep(1)
-
-
+        await self.down_resouce()
 
     async def down_resouce(self):
-        response_data = self.get_update_file()
+        response_data = await self.get_update_file()
         if response_data:
             for relative_file_path, value in response_data.items():
-                file_path = "{}\{}".format(os.getcwd(), relative_file_path)
+                file_path = f"{os.getcwd()}/{relative_file_path}"
                 if os.path.exists(file_path):
                     file_md5 = get_md5(file_path)
                     if file_md5 != value["file_md5"]:
-                        # print(file_path, "md5 不同")
-                        file_modified_time = get_modified_time(file_path)
-                        text = "md5 不同 开始下载:{}".format(file_path)
-                        print(text)
-                        download_file(url=value["url"], file_path=file_path)
+                        print(f"md5 不同 开始下载:{file_path}")
+                        await self.async_download_file(value["url"], file_path)
                     else:
-                        # print(file_path, "md5 相同")
                         pass
                 else:
-                    text = "文件不存在 开始下载:{}".format(file_path)
-                    print(text)
-                    time.sleep(1)
-                    download_file(url=value["url"], file_path=file_path)
-
+                    print(f"文件不存在 开始下载:{file_path}")
+                    await self.async_download_file(value["url"], file_path)
         else:
             print("获取更新文件内容失败")
 
-    def get_update_file(self, type="client_camera", plugins_name="plugins_A"):
-        """
-        根据类型和插件名称获取更新文件内容。
+    async def get_update_file(self, type="client_camera", plugins_name="plugins_A"):
+        """异步获取指定类型的插件文件更新信息
 
-        调用外部API查询客户端插件信息,并尝试下载指定名称的插件文件内容。
+        Args:
+            type (str, optional): 设备类型标识,默认为"client_camera"
+            plugins_name (str, optional): 需要查询的插件名称,默认为"plugins_A"
 
-        参数:
-        - type: 插件类型,默认为"client_camera"。
-        - plugins_name: 插件名称,默认为"plugins_A"。
-
-        返回:
-        - 插件文件内容字符串,如果获取成功。
-        - None,如果获取失败或出现错误。
+        Returns:
+            dict or None: 成功时返回插件文件的JSON数据,失败或未找到时返回None
         """
         url = HLM_HOST + "/api/openai/query_client_addons"
         params = {"type": type}
         try:
-            response = requests.get(url, params=params, timeout=10)
-            response.raise_for_status()
-            raw_data = response.json()
+            async with aiohttp.ClientSession() as session:
+                # 发送初始请求获取插件列表数据
+                async with session.get(url, params=params, timeout=10) as response:
+                    response.raise_for_status()
+                    raw_data = await response.json()
 
-            # 检查 raw_data 和 raw_data["data"] 是否存在
-            if not raw_data or not raw_data.get("data"):
-                return None
+                    if not raw_data or not raw_data.get("data"):
+                        return None
 
-            # 使用 next 函数简化查找逻辑
-            item = next((item for item in raw_data["data"]["list"] if item["name"] == plugins_name), None)
-            if item:
-                url = item["url"]
-                response = requests.get(url, timeout=10)
-                response.raise_for_status()
-                return json.loads(response.text)
-            return None
-        except requests.RequestException as e:
+                # 从插件列表中查找匹配的插件项
+                item = next(
+                    (item for item in raw_data["data"]["list"] if item["name"] == plugins_name),
+                    None
+                )
+                if item:
+                    # 根据插件项中的URL获取具体文件内容
+                    url = item["url"]
+                    async with session.get(url, timeout=10) as item_response:
+                        item_response.raise_for_status()
+                        return await item_response.json()
+                return None
+        except Exception as e:
             print(f"An error occurred: {e}")
             return None
+
+
+    async def async_download_file(self, url: str, file_path: str):
+        """异步下载文件"""
+        async with aiohttp.ClientSession() as session:
+            async with session.get(url) as response:
+                root_path, file_name = os.path.split(file_path)
+                check_path(root_path)
+                with open(file_path, 'wb') as f:
+                    async for chunk in response.content.iter_chunked(1024):
+                        f.write(chunk)

+ 1 - 1
python/utils/utils_func.py

@@ -41,7 +41,7 @@ def compare_two_times(time_str1, time_str2):
         return "is_same"
 
 
-def download_file(url, file_path):
+async def download_file(url, file_path):
     try:
         root_path, file_name = os.path.split(file_path)
         check_path(root_path)