简体   繁体   English

使用带有错误处理的 `with Pool() as p`

[英]using `with Pool() as p` with error handling

I've got a function being assigned to a multiprocessing pool :我有一个 function 被分配给一个多处理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))

It's altering some audio files.它正在改变一些音频文件。 One or two files are corrupted and have a sampling rate of zero.一个或两个文件已损坏且采样率为零。 This is causing a divide by zero error, which looks like this:这会导致被零除错误,如下所示:

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
"""

I'd like to handle this error, however, what I've tried doesn't seem to be working:我想处理这个错误,但是,我尝试过的似乎没有用:

def resample(old_path, new_path, sr, ext='WAV', manifest=None):
    try:
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", UserWarning)
            print(f'Loading {old_path}',)
            audio, sr = librosa.load(old_path, sr=sr)
    except ZeroDivisionError:
        audio, sr = librosa.load(old_path, sr=None)
        print(f'Error loading file at {old_path}. file=sys.stderr)

I know that the ZeroDivisionError is directly caused by the librosa.load() function, which is getting an incorrect sampling rate of 0 from one of its own dependencies.我知道 ZeroDivisionError 是由librosa.load() function 直接引起的,它从它自己的一个依赖项中获得了不正确的 0 采样率。 I have a fix for it, but I need to catch the error.我有一个修复程序,但我需要捕获错误。 How do I do that?我怎么做?

This is not the final answer, but I don't want to squeeze a lot of code in a comment:这不是最终答案,但我不想在评论中挤占大量代码:

Make the exception general and take out the retry.使异常通用并取出重试。 I want to be sure the except block is called.我想确保 except 块被调用。

Try this code:试试这个代码:

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 = librosa.load(old_path, sr=None) # remove for now
        print('Error: ', ex)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM