mozillazg / python-pinyin

汉字转拼音(pypinyin)
https://pypinyin.readthedocs.io
MIT License
4.76k stars 604 forks source link

用 vscode debugger 启动时,`import pypinyin` 语句会花费很长时间(10min左右) #322

Open LifeJustDLC opened 2 weeks ago

LifeJustDLC commented 2 weeks ago

运行环境

问题描述

用 vscode debugger 启动时,import pypinyin 语句会花费很长时间(10min左右),期间 CPU 占用略高(20%)。 正常 python ./script.py 和在 REPL 里 import 都没问题。不确定是不是 vscode debugger 的问题,总之两边都提交了 issue。

mac 上测试过没问题,问题只在 windows 上出现

(怀疑过 conda,venv,但去掉后还是一样)

问题复现步骤

用 vscode debugger 执行:

# test.py
print("before")
import pypinyin
print("after") # 此句打上断点
yafacex commented 1 week ago

我也遇到了,通过打日志发现是pinyin_dict.py那个大表卡住了,应该是debug模式下import这个大表太慢了。

mozillazg commented 1 week ago

感谢反馈!可以参考一下 https://github.com/mozillazg/python-pinyin/issues/319 这个 issue 中提供的方法,看看是否有帮助。

serfend commented 3 days ago

可以考虑把pinyin_dict.py改成加载器。

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import json

# Warning: Auto-generated file, don't edit.
with open("./test_dict.json", "r", encoding="utf-8") as f:
    pinyin_dict = json.loads(f.read())