fix: 修复浅拷贝导致的 eval 问题

This commit is contained in:
2025-12-09 14:10:08 +08:00
parent 42708e8a09
commit 23780a57c6
2 changed files with 20 additions and 8 deletions

View File

@@ -46,6 +46,7 @@ class PrecachingScreen(Screen):
for i in nucleons:
i: pt.Nucleon
i.do_eval()
#print("完成 EVAL")
def compose(self) -> ComposeResult:
yield Header(show_clock=True)
@@ -107,17 +108,21 @@ class PrecachingScreen(Screen):
def precache_by_nucleon(self, nucleon: pt.Nucleon):
"""依据 Nucleon 缓存"""
return self.precache_by_text(nucleon.metadata['formation']['tts_text'])
#print(nucleon.metadata['formation']['tts_text'])
ret = self.precache_by_text(nucleon.metadata['formation']['tts_text'])
return ret
#print(f"TTS 缓存: {nucleon.metadata['formation']['tts_text']}")
def precache_by_list(self, nucleons: list):
"""依据 Nucleons 列表缓存"""
for idx, nucleon in enumerate(nucleons):
print(f"PROC: {nucleon}")
worker = get_current_worker()
if worker and worker.is_cancelled: # 函数在worker中执行且已被取消
return False
text = nucleon.metadata['formation']['tts_text']
self.current_item = text[:30] + "..." if len(text) > 50 else text
#self.current_item = text[:30] + "..." if len(text) > 50 else text
print(text)
self.processed += 1
progress = int((self.processed / self.total) * 100) if self.total > 0 else 0
self.update_status(
@@ -131,14 +136,20 @@ class PrecachingScreen(Screen):
)
import time
time.sleep(1)
return True
return True
def precache_by_nucleons(self):
return self.precache_by_list(self.nucleons)
#print("开始缓存")
ret = self.precache_by_list(self.nucleons)
#print(f"返回 {ret}")
return ret
def precache_by_filepath(self, path: pathlib.Path):
"""预缓存单个文件的所有内容"""
return self.precache_by_list(pt.load_nucleon(path)[0])
lst = list()
for i in pt.load_nucleon(path):
lst.append(i[0])
return self.precache_by_list(lst)
def precache_all_files(self):
@@ -177,9 +188,9 @@ class PrecachingScreen(Screen):
if event.button.id == "start_precache" and not self.is_precaching:
# 开始预缓存
if self.nucleons:
self.precache_worker = self.run_worker(self.precache_by_nucleons, thread=True)
self.precache_worker = self.run_worker(self.precache_by_nucleons, thread=True, exclusive=True, exit_on_error=True)
else:
self.precache_worker = self.run_worker(self.precache_all_files, thread=True)
self.precache_worker = self.run_worker(self.precache_all_files, thread=True, exclusive=True, exit_on_error=True)
elif event.button.id == "cancel_precache" and self.is_precaching:
# 取消预缓存

View File

@@ -4,6 +4,7 @@ import heurams.services.hasher as hasher
import pathlib
import toml
import json
from copy import deepcopy
def load_nucleon(path: pathlib.Path, fmt = "toml"):
with open(path, "r") as f:
@@ -28,7 +29,7 @@ def load_nucleon(path: pathlib.Path, fmt = "toml"):
for item, attr in nested_data.items():
if item == "__metadata__":
continue
lst.append((Nucleon(hasher.hash(item), attr, nested_data['__metadata__']), nested_data["__metadata__"]["orbital"]))
lst.append((Nucleon(hasher.hash(item), attr, deepcopy(nested_data['__metadata__'])), deepcopy(nested_data["__metadata__"]["orbital"])))
return lst
def load_electron(path: pathlib.Path, fmt = "json") -> dict: