title: Django, use redis-py to lock in django views
body: <p>I'm trying to use redis to lock some of the big management Postgresql transaction I have in my project.
  I haven't been successful so far on my development environment.</p>
  
  <p>A simple version of the code would look like that:</p>
  
  <pre><code>def test_view(request):
    connec = redis.Redis(unix_socket_path='/tmp/vgbet_redis.sock')
    if not connec.setnx('test', ''):
      print 'Locked'
    else:
      time.sleep(5) #Slow transaction
      connec.delete('test')
      print 'Unlocked'
    return render_to_response("test.html")
  </code></pre>
  
  <p>If I open two tabs of that view, the first one print Unlocked after 5 seconds, then the second one prints Unlocked after 10 seconds. It looks like they are synchronous which doesn't make any sense to me.</p>
  
  <p><strong>Edit:</strong>
  I have tried to install an apache and a gevent and I got the exact same results.</p>
  
  <p>So I guess there is really something I don't understand with django + redis and my code is really wrong.</p>
  
  <p>Any help would be great.</p>
  
  <p><strong>Edit2:</strong>
  I just tried with django-redis by using redis as a cache.</p>
  
  <pre><code>CACHES = {
    'default': {
      'BACKEND': 'redis_cache.RedisCache',
      'LOCATION': '/tmp/vgbet_redis.sock',
      'OPTIONS': {
        'DB': 1,
        'PASSWORD': None,
        'PARSER_CLASS': 'redis.connection.HiredisParser'
      },
    },
  }
  </code></pre>
  
  <p>And I still have the same result if I open two tabs in my browser. The second view is blocked for 5 seconds, as if everything is synchronous.</p>
  
  <pre><code>from django.core.cache import cache
  def test_view(request):
    if cache.get('test') != None:
      print 'Locked'
    else:
      cache.set('test', '', 60)
      time.sleep(5) #Slow transaction
      cache.delete('test')
    return render_to_response("test.html")
  </code></pre>
  
  <p>If I open two terminals, I have no issue reading and writing in redis. So I really don't understand why I'm not able to use the cache in views.</p>