如何解决使用`with Poolas p`进行错误处理
我已将一个函数分配给多处理pool
:
with Pool(os.cpu_count() - 1) as p:
N = len(fpaths)
p.starmap(resample,zip(fpaths,new_paths,[sr] * N,['WAV'] * N,[manifest] * N))
它正在更改一些音频文件。一个或两个文件已损坏,采样率为零。这将导致除以零的错误,如下所示:
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "/opt/conda/lib/python3.7/multiprocessing/pool.py",line 121,in worker
result = (True,func(*args,**kwds))
File "/opt/conda/lib/python3.7/multiprocessing/pool.py",line 47,in starmapstar
return list(itertools.starmap(args[0],args[1]))
File "/home/jupyter/jn-kaggle/birdsong/who-said-what/wsw/preprocessing.py",line 35,in resample
audio,sr = librosa.load(old_path,sr=sr)
File "/opt/conda/lib/python3.7/site-packages/librosa/core/audio.py",line 172,in load
y = resample(y,sr_native,sr,res_type=res_type)
File "/opt/conda/lib/python3.7/site-packages/librosa/core/audio.py",line 553,in resample
ratio = float(target_sr) / orig_sr
ZeroDivisionError: float division by zero
"""
我想处理此错误,但是,我尝试执行的操作似乎无效:
def resample(old_path,new_path,ext='WAV',manifest=None):
try:
with warnings.catch_warnings():
warnings.simplefilter("ignore",UserWarning)
print(f'Loading {old_path}',)
audio,sr=sr)
except ZeroDivisionError:
audio,sr=None)
print(f'Error loading file at {old_path}. file=sys.stderr)
我知道ZeroDivisionError是直接由librosa.load()
函数引起的,该函数从其自身的依赖项中获取了不正确的采样率0。我有一个修复程序,但我需要抓住错误。我该怎么办?
解决方法
这不是最终答案,但我不想在注释中压缩很多代码:
将异常设为一般,然后重试。我要确保调用了except块。
尝试以下代码:
def resample(old_path,new_path,sr,ext='WAV',manifest=None):
try:
print("Start Resample: ",old_path)
with warnings.catch_warnings():
warnings.simplefilter("ignore",UserWarning)
print(f'Loading {old_path}',)
audio,sr = librosa.load(old_path,sr=sr) # maybe error here
print("Done Resample: ",old_path)
except Exception as ex: # any exception
# audio,sr=None) # remove for now
print('Error: ',ex)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。