Django Celery 缓存锁不起作用?

Django Celery cache lock did not work?

我正在尝试使用 Django 缓存来实现锁定机制。在 Celery 官方 site 中,它声称 Django 缓存对此工作正常。但是,根据我的经验,它没有用。我的经验是如果有多个threads/processes几乎同时获取锁(接近~0.003秒),所有threads/processes都会获取锁成功。对于晚于 ~0.003 秒获取锁的其他线程,它会失败。

我是唯一经历过这种情况的人吗?如果可以请指正。

def acquire(self, block = False, slp_int = 0.001):
    while True:
        added = cache.add(self.ln, 'true', self.timeout)

        if added:
            cache.add(self.ln + '_pid', self.pid, self.timeout)
            return True

        if block:
            sleep(slp_int)
            continue
        else:
            return False


# Set Django backend cache to localcache
CACHES = { 
    'default': {
        'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
        'LOCATION': '/dev/shm/django_cache',                                                                                                                                                                                              
    }   
}

问题是 Django 不保证 .add() 的原子性。 实际上 .add() 是否是原子的取决于您使用的后端。 对于 FileBasedCache.add() 不是原子的:

def add(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
    if self.has_key(key, version):
        return False
    self.set(key, value, timeout, version)
    return True

Worker A 执行 .add() 可以在 self.has_key(...) 之后但在 self.set(...) 之前被抢占。工人 B 一次执行 .add() 将成功设置密钥和 return True。当 worker A 恢复时,它还会设置密钥和 return True.

issue report 表示您查看的示例代码假设后端是 Memcached。如果您使用 Memcached 或支持原子 .add() 的后端,那么它应该可以工作。