Przeglądaj źródła

新增 异步循环的 Windows 策略避免无法在 worker 中连接CDP浏览器

mrh 1 rok temu
rodzic
commit
aa2ff97f48
1 zmienionych plików z 5 dodań i 3 usunięć
  1. 5 3
      src/tasks/crawl_asin_exract_task.py

+ 5 - 3
src/tasks/crawl_asin_exract_task.py

@@ -11,7 +11,7 @@ from utils.drission_page import ChromeOptions
 from utils.file import read_file,save_to_file
 from utils.logu import get_logger
 logger = get_logger('browser')
-
+asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
 async def async_run_extractions(crawler:Crawler, html_content):
     """异步运行提取逻辑"""
     product_info, result_table = await asyncio.gather(
@@ -68,12 +68,14 @@ async def async_process_mhtml(mhtml_path: str):
 def extra_result(self, mhtml_path: str):
     """异步Celery任务"""
     try:
+        loop = asyncio.get_event_loop()
         logger.info(f"异步任务启动: {mhtml_path}")
-        res = asyncio.run(async_process_mhtml(mhtml_path))
+        res = loop.run_until_complete(async_process_mhtml(mhtml_path))
+        # res = asyncio.run(async_process_mhtml(mhtml_path))
         logger.info(f"异步任务完成: {res}")
         return res
     except Exception as e:
-        logger.error(f"任务重试中: {str(e)}")
+        logger.exception(f"任务重试中: {str(e)}")
         self.retry(exc=e, countdown=60)
 
 def main():