postgres:更新并返回旧值,避免出现竞争情况

时间:2019-03-16 16:28:20

标签: python sql postgresql select-for-update

我需要锁定用户,直到完成其发布的功能,然后再解锁他。 到目前为止,为确保避免比赛条件,我使用过Redis。 我正在这样做:

def lock_user_was_locked(bot, update):
    sender_id = update.message.from_user.id
    chiave = "locked:"+str(sender_id)
    stato = r.incr(chiave, amount=1)
    if stato != 1:
        update.message.reply_text(tools.langMessage(getBotLang(sender_id), "too_much_match_ops"), quote=True)
        return True
    r.expire(chiave, 10)

我看到了这个答案的最后一个片段https://stackoverflow.com/a/7927957/8372336 我认为我可以使用postgres而不是redis将该查询替换为我的函数。顺便说一句,我不想​​等待用户是否被锁定,所以我正在考虑使用NOWAIT选项。 但我也想只在“ is_locked”列处于更新状态而不是整个行中时才考虑将他锁定(我也可以为其他内容添加其他列)。有没有办法只考虑更新中的该列而无需等待?

否则,我应该创建仅具有该列的另一个表来实现这一目标吗?

0 个答案:

没有答案