python - Django Celery cache lock did not work? -
i trying use django cache implement lock mechanism. in celery offical site, claimed django cache work fine this. however, in experence, did not work. experience if there multiple threads/processes acquire lock in same time (close ~0.003 second), threads/processes lock successfully. other threads acquire lock later ~0.003 second, fails.
am person experienced this? please correct me if possible.
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 localcache caches = { 'default': { 'backend': 'django.core.cache.backends.filebased.filebasedcache', 'location': '/dev/shm/django_cache', } }
the problem django makes no guarantees atomicity of .add()
. whether or not .add()
in fact atomic depends on backend using. filebasedcache
, .add()
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 executing .add()
preempted after self.has_key(...)
before self.set(...)
. worker b executing .add()
in 1 shot set key , return true
. when worker resumes, set key , return true
.
this issue report indicates example code looked @ assumes backend memcached. if use memcached or backend supports atomic .add()
should work.
Comments
Post a Comment