Python给OSError:[Errno 27]文件太大,文件太小,磁盘空间/内存足够

2024-06-26 01:35:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个应用程序,它创建几个非常小的临时文件,作为单元测试或特定功能的一部分。 我开始经历更多的“随机”错误OSError: [Errno 27] File too large。我所说的随机性是指问题有时会在重新启动时消失,或者如果我在一段时间后重新运行测试。我手动检查了Mac上的临时文件夹是否已清理,是否有足够的内存/空间来创建这样的小文件。(几个GB可用)在此上下文中的小文件例如大小为16384、58330、26502(字节)或更小。Shutil.copyfile用于创建这些文件,但在执行os.link时也会出现相同的错误,这应该占用磁盘上的最小空间。我将shutil.copfile(如果可能)替换为os.link,以测试它是否修复了问题,但效果相同。 在Mac OS上,当我进行开发时,这个错误通常是在密集运行大量测试的随机时间之后抛出的。但是,在docker映像内运行时,该错误始终持续存在

错误代码段:

    @pytest.fixture(scope="module")
    def simulate_mirror(fixtures):
        """
        Thjs fixture creates a file/directory structure that simulates an offline PyPI mirror
        Used to test the `mirror://` bandersnatch integration for scanning the whole PyPI repository
        """
        from aura import mirror as amirror

        with tempfile.TemporaryDirectory(prefix="aura_test_mirror_") as mirror:
            pmirror = Path(mirror)
            assert pmirror.is_dir()
            os.mkdir(pmirror / "json")

            for pkg, pkg_files in MIRROR_FILES.items():
                # copy the package JSON metadata
                os.link(
                    fixtures.path(f"mirror/{pkg}.json"),
                    pmirror / "json" / pkg
                )

                for p in pkg_files:
                    os.makedirs(pmirror / p["path"])
                    os.link(
                        fixtures.path(f"mirror/{p['name']}"),
>                       os.fspath(pmirror / p["path"] / p["name"])
                    )
E                   OSError: [Errno 27] File too large: '/analyzer/tests/files/mirror/wheel-0.34.2-py2.py3-none-any.whl' -> '/tmp/aura_test_mirror_a6o5p8fn/packages/8c/23/848298cccf8e40f5bbb59009b32848a4c38f4e7f3364297ab3c3e2e2cd14/wheel-0.34.2-py2.py3-none-any.whl'

tests/conftest.py:204: OSError
    def test_apip():
        # Test package taken from pip tests
        # https://github.com/pypa/pip/tree/master/tests/data/packages
        whl = Path(__file__).parent /'files' / 'simplewheel-1.0-py2.py3-none-any.whl'

        venv_dir = tempfile.mkdtemp(suffix="_pytest_aura_apip")
        # print(f'Virtualenv created in {venv_dir}')
        try:
            # Create virtualenv
            venv.create(
                env_dir=venv_dir,
>               with_pip=True,
                # symlinks=True
            )

tests/test_apip.py:56:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/usr/local/lib/python3.7/venv/__init__.py:390: in create
    builder.create(env_dir)
/usr/local/lib/python3.7/venv/__init__.py:66: in create
    self.setup_python(context)
/usr/local/lib/python3.7/venv/__init__.py:233: in setup_python
    copier(context.executable, path)
/usr/local/lib/python3.7/venv/__init__.py:176: in symlink_or_copy
    shutil.copyfile(src, dst)
/usr/local/lib/python3.7/shutil.py:122: in copyfile
    copyfileobj(fsrc, fdst)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

fsrc = <_io.BufferedReader name='/usr/local/bin/python'>, fdst = <_io.BufferedWriter name='/tmp/tmpw5gbckwv_pytest_aura_apip/bin/python'>, length = 16384

    def copyfileobj(fsrc, fdst, length=16*1024):
        """copy data from file-like object fsrc to file-like object fdst"""
        while 1:
            buf = fsrc.read(length)
            if not buf:
                break
>           fdst.write(buf)
E           OSError: [Errno 27] File too large

/usr/local/lib/python3.7/shutil.py:82: OSError

在使用venv.create创建virtualenv时,有时也会抛出这些错误。我还始终收到sqlite3.OperationalError:docker映像中的磁盘I/O错误,这可能与相同的问题有关。 更多技术信息: Mac OS Catalina已完全升级,通过brew将python重新安装到最新的3.7.7+版本,重新创建了所有virtualenv并重新安装了所有依赖项。基于其他SO问题(File too Large python),我已经检查了文件系统是否支持在限制范围内的文件大小,以及目录中允许的最大文件数。 包含问题的最新提交(包括因错误而失败的dockerfile):

https://github.com/RootLUG/aura/commit/b4c730693e8f7fd36ab2acc78997694002c4e345

触发错误的代码位置:

https://github.com/RootLUG/aura/blob/dev/tests/conftest.py#L181

https://github.com/RootLUG/aura/blob/dev/tests/test_apip.py#L54

单元测试的Travis日志:

https://travis-ci.org/github/RootLUG/aura/builds/671722230


Tags: inpytestvenvosmirrorlibusr
1条回答
网友
1楼 · 发布于 2024-06-26 01:35:58

它在Docker之外工作吗?我不熟悉他们在Mac电脑上是如何做到这一点的,但至少在Windows上,我相信Docker在虚拟机上运行时有其自身的磁盘空间限制,所以可能Docker空间不足,这就是您遇到的问题。也许值得研究一下

相关问题 更多 >