来自 Django select_for_update 的锁定记录是否受到更新值的影响

Are locked records from Django select_for_update affected by updated values

我知道如果我们有多个并发用户,select_for_update 会锁定正在查询的记录。但是是否也影响查询记录的评估呢

例如用户A和用户B同时查询table'Jobs'基于一个布尔变量'Available'。 两个用户都想访问可用作业的记录(其中 Available = True)。

用户 A 设法首先排队,他正在访问的记录现在已为用户 B 锁定。

如果用户A把所有记录都改成False,用户B是不是没有得到任何记录?或者他是否获得了与用户 A 相同的记录,但 Availability 等于 False?

此外,select_for_update() 是否与 MySQL 一起使用?或者它有不同的并发查询解决方案吗?

来自关于 select_for_update

的文档

Usually, if another transaction has already acquired a lock on one of the selected rows, the query will block until the lock is released. If this is not the behavior you want, call select_for_update(nowait=True). This will make the call non-blocking. If a conflicting lock is already acquired by another transaction, DatabaseError will be raised when the queryset is evaluated. You can also ignore locked rows by using select_for_update(skip_locked=True) instead. The nowait and skip_locked are mutually exclusive and attempts to call select_for_update() with both options enabled will result in a ValueError.

这基本上意味着当时只有一个查询通过,当它被处理时另一个查询被轮到(互斥行为)

在你的例子中考虑

...filter(available=true).select_for_update()

如果第一个更改记录,其他请求将获得新状态(没有记录首先设置为 false),因为该特定线程等待查询数据库