简体   繁体   English

如何在没有数据库的情况下使用python Transaction?

[英]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. 严格来说,除非使用TxFhttps://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.

相关问题 如何在没有甘纳许的情况下使用 python 发送以太坊交易? - How send Ethereum transaction with python without ganache? MySQL 连接器/Python:如何使用 start_transaction() 方法? - MySQL Connector/Python: How to use the start_transaction() method? 在Python SQLAlchemy中使用数据库模型对象,而不会干扰数据库事务 - Use a database model object in Python SQLAlchemy without interfering with database transactions 有没有办法将 Python 可执行文件与数据库一起使用,而无需在目标 PC 上安装数据库? - Is there a way to use a Python executable with a database without installing a database on the target PC? 如何在不使用数据库的情况下检索 json? - How to retrieve a json without the use of database? Python Sqlite3:无需使用第二个数据库即可创建模式 - Python Sqlite3: Create a schema without having to use a second database 如何在Braintree JS + Python中无计划地创建交易? - how to create transaction without plan in braintree js+python?(whiteout subscription) 如何将交易矩阵导入 Python? - How to import a transaction matrix into Python? 如何使用Python Subprocess删除数据库模式? - How to use Python Subprocess to drop a database schema? 如何通过 ApacheBeam 使用 Python 连接到 Oracle 数据库? - How to use Python to connect to an Oracle database by ApacheBeam?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM