Shutil.make_archive 线程安全吗?

Pra*_*n S 5 python archive shutil

我正在尝试使用shutil.make_archivepython 中的线程来压缩多个文件夹。我看到较小的文件夹完全压缩,同时另一个线程也停止压缩。

那么,shutil.make_archive线程安全吗?

Kov*_*Bal 5

shutil.make_archive()不是线程安全的

其原因是它更改了当前工作目录,该目录对于进程来说是全局的。线程没有自己的工作目录。参见Python 2.7中的相关代码:

save_cwd = os.getcwd()
if root_dir is not None:
    if logger is not None:
        logger.debug("changing into '%s'", root_dir)
    base_name = os.path.abspath(base_name)
    if not dry_run:
        os.chdir(root_dir)

if base_dir is None:
    base_dir = os.curdir
...
Run Code Online (Sandbox Code Playgroud)

该函数在执行开始时保存当前工作目录,并在返回之前恢复它,但这对于线程安全来说还不够。