简体   繁体   English

在 numpy.save 中写入泡菜文件时出现 FileNotFoundError

[英]FileNotFoundError when writing pickle file in numpy.save

I'm seeing a confusing intermittent error.我看到一个令人困惑的间歇性错误。 Sometimes when I call np.save I'm getting FileNotFoundError .有时当我调用np.save时,我会收到FileNotFoundError

Traceback (most recent call last):
  File "/home/leo/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line 536, in save
    pickle_kwargs=pickle_kwargs)
  File "/home/leo/anaconda3/lib/python3.7/site-packages/numpy/lib/format.py", line 629, in write_array
    pickle.dump(array, fp, protocol=2, **pickle_kwargs)
FileNotFoundError: [Errno 2] No such file or directory

During handling of the above exception, another exception occurred:

  File "/home/leo/dev/vizproc/embed.py", line 59, in save
    np.save(filename, obj)
  File "/home/leo/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line 539, in save
    fid.close()
FileNotFoundError: [Errno 2] No such file or directory

The directory it's writing to definitely exists, and the object is a dictionary with a mix of [str] and np.ndarray , so it's getting pickled on the way out.它写入的目录肯定存在,并且 object 是一个混合了[str]np.ndarray的字典,因此它在退出时会被腌制。 Looking at the numpy source, it seems that it's trying and failing to close the file it had opened for writing:查看 numpy 源代码,它似乎正在尝试关闭它为写入而打开的文件,但未能成功:

    own_fid = False
    if hasattr(file, 'read'):
        fid = file
    else:
        file = os_fspath(file)
        if not file.endswith('.npy'):
            file = file + '.npy'
        fid = open(file, "wb")
        own_fid = True

    if sys.version_info[0] >= 3:
        pickle_kwargs = dict(fix_imports=fix_imports)
    else:
        # Nothing to do on Python 2
        pickle_kwargs = None

    try:
        arr = np.asanyarray(arr)
        format.write_array(fid, arr, allow_pickle=allow_pickle,
                           pickle_kwargs=pickle_kwargs)
    finally:
        if own_fid:
            fid.close()   # <=- FileNotFoundError

and inside the format.write_array(...) call is really just some type checking and then pickle.dump(arr, fid, protocol=2, **pickle_kwargs) which is also raising FileNotFoundError .format.write_array(...)调用中实际上只是一些类型检查,然后pickle.dump(arr, fid, protocol=2, **pickle_kwargs) ,这也引发了FileNotFoundError

I'm using Numpy: 1.16.3, Python: 3.7.1 (default, Dec 14 2018, 19:28:38) [GCC 7.3.0] on Ubuntu 18.04.我正在使用Z55F00EE1DAA52B7CB5C5BC865E89DBC79Z:1.16.3,Python:3.7.1(3.7.1)

I'm trying to reason through what kind of race condition could cause this, or why else it might be happening.我试图通过什么样的竞争条件会导致这种情况进行推理,或者为什么会发生这种情况。 Is it that the file is getting opened by this process, but then another process erases the file before the writing happens?是不是这个进程打开了文件,但是另一个进程在写入发生之前擦除了文件? Seems reasonable, but then this should repro the failure, which it doesn't:似乎是合理的,但这应该会重现失败,但它不会:

fid = open("testfile", "wb")
os.unlink("testfile")
pickle.dump({'obj':'test'}, fid, protocol=2)  # no error
fid.close()  # no error

Also, after the error gets raised, there's a zero-byte file on the disk.此外,在引发错误后,磁盘上有一个零字节文件。 Any idea what's going on?知道发生了什么吗?

I believe the root cause of this was a hardware problem or something deep in the disk system.我相信这个问题的根本原因是硬件问题或磁盘系统深处的问题。 Frustratingly, the OS wasn't logging any error messages.令人沮丧的是,操作系统没有记录任何错误消息。 A bunch of details in this github issue including a simpler repro case, in case anybody wants to dive in. Key points: it only happened on USB-attached disks, and only if the path had a symlink in it.这个 github 问题中有很多细节,包括一个更简单的复制案例,以防有人想深入研究。关键点:它只发生在 USB 连接的磁盘上,并且只有路径中有符号链接。

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

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