def process(command): client = RedisClient(host='localhost', port=6391) client.select(2) proposal_id_lock = "proposal_id_lock" proposal_id_key = "uuid" proposal_id = 0 #get proposal id try: with RedisLock(client, proposal_id_lock, timeout=lock_timeout) as lock: proposal_id = client.get(proposal_id_key) client.set(proposal_id_key, int(proposal_id) + 1) except LockNotAcquired: return reply(False, command) proposeal_key = "proposal" + str(proposal_id) client.hset(proposeal_key, 'title', command['title']) client.hset(proposeal_key, 'content', command['content']) #提议: 复议/未复议 #复议: 赞同/反对 #会议: 进行中/已完成/为开始 #决议: 执行 client.hset(proposeal_key, 'state', 0) client.hset(proposeal_key, 'type', 0) client.hset(proposeal_key, 'time', time.time()) client.lpush("list", proposal_id) #set proposal title #proposal_id: title: *** # content: *** # state: *** # time: #SORT list get proposal_*->time #sort list by proposal*->time get proposal*->content return reply(True, command, proposal_id)
def take_lock(): global counter client = RedisClient('localhost', 6379) try: with RedisLock(client, key, timeout=lock_timeout) as lock: v = client.get(incr_key) sleep(random.random() * sleep_factor) client.set(incr_key, int(v) + 1) counter += 1 except LockNotAcquired: pass
def main(): client = RedisClient('localhost', 6379) client.delete(key) client.set(incr_key, 0) for _ in xrange(500): fork(take_lock) if random.random() > 0.1: sleep(random.random() / 10) sleep(2) assert counter == int(client.get(incr_key)), 'Incr failed!' quickstop()
def process(command): #get account id client = RedisClient('localhost', 6391) client.select(1) accout_id_key = "accout_id" accout_id_lock = "accout_id_lock" try: with RedisLock(client, accout_id_lock, timeout=lock_timeout) as lock: accout_id = client.get(accout_id_key) client.set(accout_id_key, int(accout_id) + 1) except LockNotAcquired: pass #create account: name password privilege return {"result":False, "user_id":0, "privilege":0}