Python给OSError:[Errno 27]文件在非常小的文件上太大,具有足够的磁盘空间/内存可用

问题描述 投票:0回答:1

我有一个应用程序,可以创建多个非常小的临时文件,作为单元测试或特定功能的一部分。我开始经常遇到“随机”错误OSError: [Errno 27] File too large。随机地,我的意思是问题有时会在重新启动后消失,或者如果我在一段时间后重新运行测试,则该问题会消失。我手动检查了Mac上的temp文件夹是否已清理并且有足够的内存/空间来创建此类小文件。 (可用GB数)在这种情况下,小文件的大小例如为16384、58330、26502(以字节为单位)或更小。 Shutil.copyfile用于创建这些文件,但在仅执行os.link时也会产生相同的错误,该错误应占用磁盘上的最小空间。我用os.link替换了shutil.copfile(如果可能),以测试它是否可以解决问题,但效果相同。在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:泊坞窗映像中的磁盘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

python docker filesize oserror
1个回答
0
投票

它是否可以在Docker之外使用?我不熟悉它们在Mac上的操作方式,但至少在Windows上,我相信Docker在具有自身磁盘空间限制的VM中运行,因此Docker可能空间不足,这就是您要面对的问题。可能值得研究。

© www.soinside.com 2019 - 2024. All rights reserved.