[英]How to use python Transaction without database ?
I have two line in my code which first one is os.unlink
and second one is os.symlink
. 我的代码中有两行,第一行是
os.unlink
,第二行是os.symlink
。 like : 喜欢 :
os.unlink(path)
os.symlink(new_path)
The sequence should not be change, The problem is, some times it unlink a file (in other word it remove it's shortcut) but second line could not create symbolic link (do to some addressing issue). 顺序不应该更改,问题是,有时它取消链接文件(换句话说,它删除了快捷方式),但是第二行无法创建符号链接(解决了某些寻址问题)。
My question is: Is there any all or non transaction tool like the one we have in database, to do both line or non ? 我的问题是:是否有像我们在数据库中那样的全部或非交易工具同时执行行或非行?
you could try this: 您可以尝试以下方法:
import os
linkname = '/tmp/test.lnk'
orig_target = os.path.realpath(linkname)
os.unlink(linkname)
try:
os.symlink(new_target, linkname)
except:
os.symlink(orig_target, linkname)
maybe check what exceptions can occur and only catch the ones that are relevant. 也许检查哪些异常可能发生并且仅捕获相关的异常。
Strictly speaking it is not possible unless you use Transactional filesystem like TxF ( https://en.wikipedia.org/wiki/Transactional_NTFS ) because nothing prevents your machine from poweroff between two commands. 严格来说,除非使用TxF ( https://en.wikipedia.org/wiki/Transactional_NTFS )之类的事务性文件系统,否则这是不可能的,因为不会阻止您的计算机在两个命令之间断电。
I can see 2 ways here: 我在这里可以看到2种方式:
1) Switch to Database 1)切换到数据库
2) Check all conditions before unlinking. 2)断开连接之前,请检查所有条件。 What prevents you from symlinking?
是什么阻止您进行符号链接?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.