From c199f3bd80b40909811cbf30dcc60c8a3e8e3df5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B0=B4=E5=B1=85=E5=AE=A4?= Date: Wed, 22 Feb 2023 12:10:03 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Use=20a=20with=20statement=20to?= =?UTF-8?q?=20ensure=20threads=20are=20cleaned=20up=20promptly?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/gacha_log/log.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/modules/gacha_log/log.py b/modules/gacha_log/log.py index 49fa1be..b0206e6 100644 --- a/modules/gacha_log/log.py +++ b/modules/gacha_log/log.py @@ -198,10 +198,13 @@ class GachaLog: temp_id_data = { pool_name: [i.id for i in pool_data] for pool_name, pool_data in gacha_log.item_list.items() } - # 使用新进程进行遍历,避免堵塞主进程 - executor = ThreadPoolExecutor() + # 使用新线程进行遍历,避免堵塞主线程 loop = asyncio.get_event_loop() - new_num = await loop.run_in_executor(executor, self.import_data_backend, all_items, gacha_log, temp_id_data) + # 可以使用with语句来确保线程执行完成后及时被清理 + with ThreadPoolExecutor() as executor: + new_num = await loop.run_in_executor( + executor, self.import_data_backend, all_items, gacha_log, temp_id_data + ) for i in gacha_log.item_list.values(): # 检查导入后的数据是否合法 await self.verify_data(i)