feat: GUI 面板专题图生成支持多线程并发克里金

- step11_map_panel.run() 串行 for 循环改为 ThreadPoolExecutor 并发
- 默认 2 线程并行,通过过滤已存在文件跳过重复生成
- 支持取消信号:_done Event 通知 worker 线程提前退出
- 线程安全:_lock 保护共享计数器,Qt 信号从 QThread 安全 emit
- 修复 Tuple 类型导入缺失
This commit is contained in:
duxin
2026-07-28 16:45:46 +08:00
parent e8a81008fa
commit aa5d9d9392

View File

@ -7,7 +7,7 @@ Step10 面板 - 专题图生成
import os import os
import traceback import traceback
from pathlib import Path from pathlib import Path
from typing import List, Optional from typing import List, Optional, Tuple
from src.gui.panels._step_path_resolver import resolve_subdir, get_step_output_path, scan_work_dir_for_input from src.gui.panels._step_path_resolver import resolve_subdir, get_step_output_path, scan_work_dir_for_input
@ -100,40 +100,104 @@ class Step11MapBatchThread(QThread):
f"[警告] 共享上下文失败: {e},回退逐个处理", "warning" f"[警告] 共享上下文失败: {e},回退逐个处理", "warning"
) )
# ── 批量处理 ── # ── 批量处理(多线程并发克里金插值)──
for i, csv_p in enumerate(self.csv_paths): # 克里金内部 numpy/scipy 运算释放 GILThreadPoolExecutor 真并行
if self._cancelled: import concurrent.futures
self.log_message.emit("专题图批量任务已被用户取消", "warning") import threading
break
self.progress.emit(i + 1, n)
self.log_message.emit(f"专题图 [{i + 1}/{n}] {csv_p}", "info")
_all_csvs = list(self.csv_paths) # 快照副本
# 过滤已存在的文件
_pending: List[Tuple[str, str]] = [] # [(csv_path, output_file), ...]
_skipped = 0
for csv_p in _all_csvs:
stem = Path(csv_p).stem stem = Path(csv_p).stem
output_file = ( out_f = (
str(Path(self.output_dir_optional) / f'{stem}_distribution.png') str(Path(self.output_dir_optional) / f'{stem}_distribution.png')
if self.output_dir_optional if self.output_dir_optional
else None else None
) )
# 已存在则跳过(兼容 tif 重定向后的文件名) if out_f and (
if output_file and ( Path(out_f).exists()
Path(output_file).exists() or Path(out_f).with_suffix('.tif').exists()
or Path(output_file).with_suffix('.tif').exists()
): ):
self.log_message.emit(f" → 跳过(已存在)", "info") _skipped += 1
continue self.log_message.emit(f" → 跳过(已存在): {stem}", "info")
else:
_pending.append((csv_p, out_f))
try: if _skipped > 0:
mapper.process_data( self.log_message.emit(
csv_file=csv_p, f"[跳过] {_skipped} 个已存在,待处理 {len(_pending)}", "info")
shp_file=boundary_shp,
output_file=output_file, _n_pending = len(_pending)
resolution=resolution, if _n_pending == 0:
output_format='tif', self.log_message.emit("所有专题图均已存在,无需重新生成", "info")
shared_context=shared_ctx, self.finished_ok.emit(n)
) return
except Exception as e:
self.log_message.emit(f" → 失败: {e}", "error") _max_workers = max(1, min(2, _n_pending, os.cpu_count() or 4))
continue _completed = 0
_lock = threading.Lock()
_done = threading.Event() # 取消信号
def _process_one(csv_path: str, output_file: str):
"""单个 CSV → 分布图(在 ThreadPoolExecutor 线程中执行)"""
if _done.is_set():
return None, csv_path
mapper.process_data(
csv_file=csv_path,
shp_file=boundary_shp,
output_file=output_file,
resolution=resolution,
output_format='tif',
shared_context=shared_ctx,
)
return output_file, csv_path
if _max_workers > 1 and _n_pending > 1:
self.log_message.emit(
f"[并发] {_n_pending} 个专题图,{_max_workers} 线程并行克里金", "info")
with concurrent.futures.ThreadPoolExecutor(
max_workers=_max_workers) as executor:
_futures = {
executor.submit(_process_one, csv_p, out_f): (csv_p, out_f)
for csv_p, out_f in _pending
}
for _future in concurrent.futures.as_completed(_futures):
if self._cancelled:
_done.set()
self.log_message.emit("专题图批量任务已被用户取消", "warning")
executor.shutdown(wait=False, cancel_futures=True)
break
csv_p, _ = _futures[_future]
try:
_future.result()
with _lock:
_completed += 1
self.progress.emit(_completed + _skipped, n)
self.log_message.emit(
f"专题图 [{_completed}/{_n_pending}] ✓: "
f"{Path(csv_p).name}", "info")
except Exception as e:
with _lock:
_completed += 1
self.log_message.emit(
f"专题图 [{_completed}/{_n_pending}] ✗: "
f"{Path(csv_p).name}{e}", "error")
else:
for csv_p, out_f in _pending:
if self._cancelled:
self.log_message.emit("专题图批量任务已被用户取消", "warning")
break
_completed += 1
self.progress.emit(_completed + _skipped, n)
self.log_message.emit(
f"专题图 [{_completed}/{_n_pending}] {csv_p}", "info")
try:
_process_one(csv_p, out_f)
except Exception as e:
self.log_message.emit(f" → 失败: {e}", "error")
continue
self.finished_ok.emit(n) self.finished_ok.emit(n)
except Exception as e: except Exception as e: