python的shutil.copyfile()是原子的吗? [英] Is python's shutil.copyfile() atomic?

查看:206
本文介绍了python的shutil.copyfile()是原子的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个python脚本,该脚本使用 shutil复制文件.在Linux上为copyfile().在复制过程中,其他进程可能正在尝试读取文件.以下内容足以确保外部进程不会损坏文件的视图吗?

I'm writing a python script that copies a file using shutil.copyfile() on Linux. During the copying, other processes may be trying to read the file. Is the following sufficient to ensure that an external process doesn't get a corrupted view of the file?

os.unlink(dest)
shutil.copyfile(src, dest)

也就是说,shutil.copyfile()是原子的,以便其他进程在复制操作完成之后才能读取目标文件吗?

That is, is shutil.copyfile() atomic such that other processes cannot read the destination file until after the copy operation is complete?

推荐答案

不,似乎只是对于原子复制操作,应将文件复制到同一文件系统上的其他位置,然后将os.rename()复制到所需的位置(在Linux中,保证 是原子的)

For an atomic copy operation, you should copy the file to a different location on the same filesystem, and then os.rename() it to the desired location (which is guaranteed to be atomic on Linux).

这篇关于python的shutil.copyfile()是原子的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆