Django Celery cache lock did not work?

帅比萌擦擦* 提交于 2019-12-01 22:43:03

The problem is that Django makes no guarantees as to the atomicity of .add(). Whether or not .add() will in fact be atomic depends on the backend you are using. With a FileBasedCache, .add() is not atomic:

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 executing .add() could be preempted after self.has_key(...) but before self.set(...). Worker B executing .add() in one shot would successfully set the key and return True. When worker A resumes, it would also set the key and return True.

This issue report indicates that the example code you looked at assumes that the backend is Memcached. If you use Memcached or a backend that supports an atomic .add() then it should work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!