import os from multiprocessing import Pool from pathlib import Path from lunar_calendar import LunarCalendar def process_year(year, output_dir): try: cal = LunarCalendar(year) out_file = output_dir / f"{year}.json" # 如果不想覆寫,再加個 exists() 檢查 if not out_file.exists(): cal.save_json(str(out_file)) print(f"完成,已輸出:{out_file}") else: print(f"跳過,檔案已存在:{out_file}") except Exception as e: print(f"處理 {year} 時出錯:{e}") def main(): output_dir = Path('lunar_json') output_dir.mkdir(parents=True, exist_ok=True) # years = range(1851, 2149) years = range(1, 2149) with Pool() as pool: pool.starmap(process_year, [(y, output_dir) for y in years]) if __name__ == "__main__": main()