Ejemplo n.º 1
0
 def lock(self):
     if self.locked == True:
         return
     import datetime as dt
     mc = mcClient()
     mc.add(key=self.key + "@lockedAt", value=dt.datetime.now(), namespace="Mutex")
     v = mc.incr(self.key, namespace="Mutex", initial_value=1)
     while v != 1:
         mc.decr(self.key, namespace="Mutex")
         lastLockedAt = mc.get(self.key + "@lockedAt", namespace="Mutex")
         if lastLockedAt is None or \
             (dt.datetime.now() - lastLockedAt > dt.timedelta(seconds=self.maxTimeOut*4)):
             mc.set(key=self.key, value=0, namespace="Mutex")
         else:
             random.seed(time.time())
             time.sleep(random.random() * self.maxTimeOut)
         v = mc.incr(self.key, namespace="Mutex", initial_value=1)
     mc.set(key=self.key + "@lockedAt", value=dt.datetime.now(), namespace="Mutex")
     self.locked = True
Ejemplo n.º 2
0
import logging
import random
import time

from google.appengine.api.memcache import Client as mcClient

mc = mcClient()
NS = "mtx"

class Mutex:
  """Simple mutex mechanism.
  
  A fork of:
  http://appengine-cookbook.appspot.com/recipe/mutex-using-memcache-api/
  
  Example use:
    m = Mutex("Some Key", 2)
    try:
      m.lock()
      # Do stuff here....
    except:
      # Handle errors here....
      pass
    finally:
      m.unlock()
  """
  def __init__(self, key, mem_timeout=30):
    self.key = key
    self.mem_timeout = mem_timeout
    self.locked = False
    random.seed()
Ejemplo n.º 3
0
import logging
import random
import time

from google.appengine.api.memcache import Client as mcClient

mc = mcClient()
NS = "mtx"


class Mutex:
    """Simple mutex mechanism.
  
  A fork of:
  http://appengine-cookbook.appspot.com/recipe/mutex-using-memcache-api/
  
  Example use:
    m = Mutex("Some Key", 2)
    try:
      m.lock()
      # Do stuff here....
    except:
      # Handle errors here....
      pass
    finally:
      m.unlock()
  """

    def __init__(self, key, mem_timeout=30):
        self.key = key
        self.mem_timeout = mem_timeout
Ejemplo n.º 4
0
 def unlock(self):
     if self.locked == True:
         mc = mcClient()
         mc.decr(self.key, namespace="Mutex")
         self.locked = False