如何在pymongo中使用fsyncLock()和fsyncUnlock()锁定并发写操作

编程入门 行业动态 更新时间:2024-10-26 00:28:58
本文介绍了如何在pymongo中使用fsyncLock()和fsyncUnlock()锁定并发写操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试同时从我的收藏中扣除金额,因此我需要保持金额一致.

I am trying to deduct the amount from my collection concurrently so I need to maintain the amount consistent.

示例:

My balance -> 1000 User 1 -> trying to deduct 700 User 2 -> trying to deduct 800 (Both users are accessing same account using different device)

他们试图同时借记金额.所以我需要并发

they try to debit the amount at the same time . So I need concurrency

在 mongoDB 中,我们可以使用 fsyncLock() 和 fsyncUnlock(),但我需要在 pymongo (Python) 模块中使用此功能.如何实现此功能?

In mongoDB we can use fsyncLock() and fsyncUnlock() but I need to use this feature in pymongo (Python) module. How can I achieve this feature?

推荐答案

db.fsyncLock() 和 db.fsyncUnlock() 是 pymongo 不提供的 mongo shell 方法.相反,您可以调用 fsync 管理命令对 admin 数据库执行相同的操作.类似的东西:

db.fsyncLock() and db.fsyncUnlock() are mongo shell methods not available from pymongo. Instead, you can call to the fsync administrative command to perform the same operation against the admin database. Something like:

import pymongo db_host='__put_your_ip_or_hostname_here__' client = pymongo.MongoClient(db_host) db = client['admin'] dbmand("fsync", lock=True)

但是,正如 kevinadi 在评论中所说,使用事务进行并发而不是 fsynclock.当数据和日志文件夹位于不同的卷/文件系统时,此命令用于执行文件系统级备份.

But, as kevinadi said in the comments, Use transactions for concurrency instead of fsynclock. This command is intended to be used to perform filesystem-level backups when the data and journal folders lives separate volumes/filesystems.

更多推荐

如何在pymongo中使用fsyncLock()和fsyncUnlock()锁定并发写操作

本文发布于:2023-11-10 01:57:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574040.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:操作   如何在   fsyncLock   pymongo   fsyncUnlock

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!