def _depart(self, player, disconnected): """ Removes a player from the table and updates all the other players. """ self.players.remove(player) msg = Message('DEPARTED') # Let everyone know msg.player_name = player.name # which player is leaving, msg.disconnected = disconnected # and if they disconnected. self.send(msg) # The only time DEPART causes a turn to shift is when the current # artist leaves. The instance that receives the DEPART will always have # the departing player. Therefore it is safe to do the _pass_turn call # here. artist = self._get_artist() if player.name == artist: self._pass_turn(player.name) # Remove player from the player and turn lists redis.zrem(self.players_key, player.name) redis.zrem(self.turns_key, player.name) if not self.players: self.pubsub.unsubscribe(self.topic) # No players? Unsubscribe from # further updates. self.manager.remove_table(self.name) self.alive = False
def acquire_semaphore(redis, semname, limit, timeout=10): """ 每当锁或者信号量因为系统时钟的细微不同而导致的获取结果出现剧烈变化时,这个锁或者信号量就是不公平的(unfair). 不公平的锁和信号量可能会导致客户端永远也无法获得它原本应该得到的锁或信号量。 :param redis: :param semname: :param limit: :param timeout: :return: """ semname = "semaphore:" + semname ident = str(uuid.uuid4()) now = time.time() p = redis.pipeline(True) # 清理过期的信号量持有者 p.zremrangebyscore(semname, '-inf', now-timeout) # 尝试获取信号量 p.zadd(semname, {ident: now}) p.zrank(semname, ident) res = p.execute() if res[-1] < limit: return ident # 获取信号量失败,删除之前添加的标识符 redis.zrem(semname, ident) return None
def handler(event, context): for record in event['Records']: if record['eventName'] == "REMOVE": continue print(record) playerName = record['dynamodb']['NewImage']['PlayerName']['S'] if 'OldImage' in record['dynamodb'].keys(): oldScore = int(record['dynamodb']['OldImage']['Score']['N']) oldWin = int(record['dynamodb']['OldImage']['Win']['N']) oldLose = int(record['dynamodb']['OldImage']['Lose']['N']) olddata = { "PlayerName": str(playerName), "Win": int(oldWin), "Lose": int(oldLose) } old_json = json.dumps(olddata) redis.zrem('Rating', old_json) newScore = int(record['dynamodb']['NewImage']['Score']['N']) newWin = int(record['dynamodb']['NewImage']['Win']['N']) newLose = int(record['dynamodb']['NewImage']['Lose']['N']) newdata = { "PlayerName": str(playerName), "Win": int(newWin), "Lose": int(newLose) } new_json = json.dumps(newdata) redis.zadd('Rating', new_json, newScore) return "OK"
def handler(event, context): for record in event['Records']: if record['eventName'] == "REMOVE": continue print(record) playerName = record['dynamodb']['NewImage']['PlayerName']['S'] if 'OldImage' in record['dynamodb'].keys(): oldScore = int(record['dynamodb']['OldImage']['Score']['N']) oldWin = int(record['dynamodb']['OldImage']['Win']['N']) oldLose = int(record['dynamodb']['OldImage']['Lose']['N']) olddata = { "PlayerName" : str(playerName), "Win" : int(oldWin), "Lose" : int(oldLose)} old_json = json.dumps(olddata) redis.zrem('Rating', old_json) newScore = int(record['dynamodb']['NewImage']['Score']['N']) newWin = int(record['dynamodb']['NewImage']['Win']['N']) newLose = int(record['dynamodb']['NewImage']['Lose']['N']) newdata = { "PlayerName" : str(playerName), "Win" : int(newWin), "Lose" : int(newLose)} new_json = json.dumps(newdata) redis.zadd('Rating', new_json, newScore) return "OK"
def _depart(self, player, disconnected): """ Removes a player from the table and updates all the other players. """ self.players.remove(player) msg = Message('DEPARTED') # Let everyone know msg.player_name = player.name # which player is leaving, msg.disconnected = disconnected # and if they disconnected. self.send(msg) # The only time DEPART causes a turn to shift is when the current # artist leaves. The instance that receives the DEPART will always have # the departing player. Therefore it is safe to do the _pass_turn call # here. artist = self._get_artist() if player.name == artist: self._pass_turn(player.name) # Remove player from the player and turn lists redis.zrem(self.players_key, player.name) redis.zrem(self.turns_key, player.name) if not self.players: self.pubsub.unsubscribe(self.topic) # No players? Unsubscribe from # further updates. self.manager.remove_table(self.name) self.alive = False
def delete_message_by_value(text, all=False): if not all: if redis.zrem('message', text): print('删除成功', text) else: c = input('你确认删除所有的消息吗yes/no') if c == 'yes': messages = redis.zrange('message', 0, -1) for m in messages: if redis.zrem('message', m): print('删除成功', m) else: print('删除取消')
def poll_queue(redis): while 1: item = redis.zrange('delayed:queue:', 0, 0, withscores=True) # 任务集合没有任务,或者任务的执行时间未到。 if not item or item[0][1] > time.time(): time.sleep(0.01) continue # 解码要被执行的任务,弄清除它应该被推入哪个任务队列。 item = item[0][0] identifier, queue, name, args = json.loads(item) # 为了对任务进行移动尝试获取锁 # 细粒度锁 locked = acquire_lock(redis, identifier) if not locked: continue # 将任务推入适当的任务队列里面。 if redis.zrem('delayed:queue:', item): redis.lpush('queue:' + queue, item) release_lock(redis, identifier, locked) ##################################################################################################################### # 延迟的优先级任务队列 # ['high-delayed', 'medium-delayed', 'low-delayed']
def suggest_product_for(self, products, max_results=6): product_ids = [product.id for product in products] if len(products) == 1: suggestions = redis.zrange(self.get_product_key(product_ids[0]), 0, -1, desc=True)[:max_results] else: flat_ids = ''.join([str(id) for id in product_ids]) tmp_key = 'tmp_{}'.format(flat_ids) keys = [self.get_product_key(id) for id in product_ids] redis.zunionstore(tmp_key, keys) redis.zrem(tmp_key, *product_ids) suggestions = redis.zrange(tmp_key, 0, -1, desc=True)[:max_results] redis.delete(tmp_key) suggested_product_ids = [int(id) for id in suggestions] suggested_products = list(Product.objects.filter(id__in=suggested_product_ids)) suggested_products.sort(key=lambda x: suggested_product_ids.index(x.id)) return suggested_products
def article_switch_vote(redis, user, from_article, to_article): # HOMEWORK 2 Part I from_article_id = from_article.split(':')[-1] to_article_id = to_article.split(':')[-1] redis.srem('voted:' + from_article_id, user) redis.sadd('voted:' + to_article_id, user) # or redis.smove('voted:' + from_article_id, 'voted:' + to_article_id, user) from_article_score = redis.zscore('score:', 'article:' + from_article_id) changed_from_article_score = from_article_score - 432 to_article_score = redis.zscore('score:', 'article:' + to_article_id) changed_to_article_score = to_article_score + 432 redis.zrem('score:', 'article:' + from_article_id) redis.zadd('score:', changed_from_article_score, 'article:' + from_article_id) redis.zrem('score:', 'article:' + to_article_id) redis.zadd('score:', changed_to_article_score, 'article:' + to_article_id)
def loop(): values = redis.zrangebyscore("delay-queue", 0, time.time(), start=0, num=1) if not values: time.sleep(1) # continue value = values[0] success = redis.zrem("delay-queue", value) if success: msg = json.loads(value) handle_msg(msg)
def release_semaphore(redis, semname, identifier): """ :param redis: :param semname: :param identifier: :return: """ # 如果信号量被正确删除则返回True; 返回False则表示该信号量已经因为过期而被删除了。 semname = "semaphore:" + semname return redis.zrem(semname, identifier)
def loop9): while true: values = redis.zrangebyscore('delay-queue', 0, time.time(), start=1, num=1) if not values: time.sleep(1) continue value = values[0] success = redis.zrem('delay-queue', value) if success: msg = json.loads(value) handle_msg(msg)
def loop(self): while True: # 返回score位于0到time.time()之间的value(默认从小到大排序), 返回索引位于start和num之间的集合,左闭右开 values = redis.zrangebyscore("delay-queue", 0, time.time(), start=0, num=1) if not values: time.sleep(1) continue value = values[0] success = redis.zrem("delay-queue", value) if success: msg = json.loads(value) handle_msg(msg)
def loop(): while True: # 最多取一条 values = redis.zrangebyscore("delay-queue", 0, time.time(), start=0, num=1) if not values: time.sleep(1) # 延迟队列空的,休息1s continue value = values[0] # 获取第一条,也只有一条 # Redis 的zrem是多线程多进程任务的关键,zrem的返回值决定当前实例有没有抢到任务,因为loop方法可能会被多个线程、多个进程调度,同一个任务可能会被多个进程线程抢到,通过zrem来决定唯一成功的线程. success = redis.zrem("delay-queue", value) # 从消息队列中移除该消息 if success: # 因为有多线程并发问题,最终只会有一个进程可以抢到消息 msg = json.loads(value) handle_msg(msg) # 需要对handle_msg进行异常捕获,避免因为个别任务处理问题导致循环异常退出
def on_close(self): #Connection.clients[str(self._address[0])+ str(self._address[1])].stream.close() del Connection.clients[self.uuid] redis.zrem('hs:user:session' + ":" + server_id, self.uuid) redis.zrem('hs:user:search' + ":" + server_id, self.uuid) redis.zrem('hs:room:user' + ":" + server_id, self.uuid) """ for u, client in Connection.clients.items(): encoded_data = {"uuid":u, "service": "main", "method": "leavePlayer", "params":{"user": {'uuid':self.uuid}}} reqt= request(encoded_data) res= reqt.dump() #client.stream.write((''.join(res)).encode('utf8')) client.stream.write(res) """ print "A user has left.", self.uuid, self._address
import redis import threading import requests endpoint = "http://127.0.0.1:8000" redis = redis.Redis(host="localhost", port = 6900) def response_ser(server, data): resp = requests.get(server, data) print(f'{resp} is the server response for {data} being pushed') while True: try: data = redis.zrange('queue',-1,-1) if len(data): redis.zrem('queue',data[0]) t = threading.Thread(target = response_ser, args = [endpoint, data[0]]) t.start() except: print('ERROR!') pass ~ ~ ~
def release_semaphore(redis, semname, identifier): # 如果信号量被正确删除则返回True; 返回False则表示该信号量已经因为过期而被删除了。 semname = "semaphore:" + semname return redis.zrem(semname, identifier)
def die(user): redis.zrem("leaderboard", user) return user + " is dead!"
'Bearer 041fff2f1e9950579315d9a8d629ef9f-5b7c44123e8fc34c65951f4d3332b96b' }) responce_json = response.json() price = responce_json['prices'][0] loc = dateutil.parser.parse(price['time']) now_price['time'] = int(loc.timestamp() / 60) now_price['close'] = float(price['bids'][0]['price']) last_no = redis.zcard("fx") - 1 db_price = json.loads(redis.zrange("fx", last_no, last_no)[0]) if now_price['time'] == db_price['time'] and same < 4 * 60: now_price['no'] = db_price['no'] redis.zrem("fx", json.dumps(db_price)) chart = {} chart[json.dumps(now_price)] = last_no redis.zadd("fx", chart) same += 1 elif now_price['time'] != db_price['time']: now_price['no'] = db_price['no'] + 1 chart = {} chart[json.dumps(now_price)] = last_no redis.zadd("fx", chart) print("rate : %s %d %d %6.3f" % (loc.astimezone(JST), now_price['no'], now_price['time'], now_price['close'])) same = 0 except Exception as e: print(e)
def delete(proxy_ids): for id in proxy_ids: redis.delete(Proxy._get_redis_key(id)) redis.srem(Proxy._get_set_key(), id) redis.zrem(Proxy._get_sorted_set_key(), id)