python - 如何锁定 django 命令以进行单次运行。 Django 。 Python

标签 python django

如何锁定 django 命令,使其不会同时运行两次?

最佳答案

在某处创建一个锁 - 我最近看到使用缓存的非常简单的锁实现:

LOCK_EXPIRE = 60 * 5

lock_id = "%s-lock-%s" % (self.name, id_hexdigest) #computed earlier

is_locked = lambda: str(cache.get(lock_id)) == "true"
acquire_lock = lambda: cache.set(lock_id, "true", LOCK_EXPIRE)
release_lock = lambda: cache.set(lock_id, "nil", 1)

if not is_locked():
    aquire_lock()
    try:
        #do something
    finally:
        release_lock()

这只是众多可能的实现之一。

编辑:已更正,我只是不假思索地粘贴了代码。 try...finally block 用于确保无论发生什么情况,锁始终被释放 - 但当然 if 语句也是必要的。

关于python - 如何锁定 django 命令以进行单次运行。 Django 。 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3459631/

相关文章:

python - 如何将数据从文本操作为 CSV

python - 属性错误: 'NoneType' object has no attribute '_consistency' when trying to show a list from database

python - 如何使用 Python 在 ElasticSearch 中创建嵌套索引?

Python 2.7 cx_freeze : What are the risks of removing the encodings that I dont need?

django - 如何合并多个查询集并删除重复项?

django - 从 Django 中的外键预填充 slug 字段

python - 如何呈现其中包含 UTF8 字符的 Django 模板?

python - 想要向现有表中添加一对一的列

python - Seaborn 属性错误 : module 'seaborn' has no attribute 'displot' AND conda Solving environment fail

python - 我的 Raspberry Pi 上的 Python 没有计数功能