代码之家  ›  专栏  ›  技术社区  ›  rocksNwaves

使用'with Pool()作为p`进行错误处理

  •  0
  • rocksNwaves  · 技术社区  · 4 年前

    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, 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)
    

    我知道零除法误差是由 librosa.load()

    0 回复  |  直到 4 年前
        1
  •  0
  •   Mike67    4 年前

    这不是最终的答案,但我不想在评论中挤出太多代码:

    把这个例外排除在外。我想确定是否调用了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 = librosa.load(old_path, sr=None) # remove for now
            print('Error: ', ex)