async def test_execute_pubsub_errors(create_connection, server): sub = await create_connection(server.tcp_address) with pytest.raises(TypeError): sub.execute_pubsub('subscribe', "chan:1", None) with pytest.raises(TypeError): sub.execute_pubsub('subscribe') with pytest.raises(ValueError): sub.execute_pubsub('subscribe', Channel('chan:1', is_pattern=True)) with pytest.raises(ValueError): sub.execute_pubsub('unsubscribe', Channel('chan:1', is_pattern=True)) with pytest.raises(ValueError): sub.execute_pubsub('psubscribe', Channel('chan:1', is_pattern=False)) with pytest.raises(ValueError): sub.execute_pubsub('punsubscribe', Channel('chan:1', is_pattern=False))
async def subscribe(self, channelname): channel = Channel(channelname, is_pattern=False, loop=self.__loop) await self.__redisconn.subscribe(channel) return self.__redisconn.channels[channelname]
async def subscribe_to_redis(redis_url, channel_name): """async redis channel subscribe """ r = await create_connection(redis_url) channel = Channel(channel_name, is_pattern=False) await r.execute_pubsub('subscribe', channel) return channel, r
async def subscribe_to_redis(path): conn = await create_connection(('localhost', 6379)) # Set up a subscribe channel channel = Channel(f'{path}', is_pattern=False) await conn.execute_pubsub('subscribe', channel) return channel, conn
async def reader(message_queue: aioredis.Channel, game_id: str) -> None: global channels logger.info(f"Started listening to {game_id}") async for message in message_queue.iter(encoding="utf-8"): logger.info(f"Broadcasting to {game_id}: {str(message)}") for websocket in channels[game_id]: await websocket.send(str(message)) logger.info(f"Stopped listening to {game_id}")
def test_execute_pubsub_errors(create_connection, loop, server): sub = yield from create_connection(server.tcp_address, loop=loop) with pytest.raises(TypeError): sub.execute_pubsub('subscribe', "chan:1", None) with pytest.raises(TypeError): sub.execute_pubsub('subscribe') with pytest.raises(ValueError): sub.execute_pubsub('subscribe', Channel('chan:1', is_pattern=True, loop=loop)) with pytest.raises(ValueError): sub.execute_pubsub('unsubscribe', Channel('chan:1', is_pattern=True, loop=loop)) with pytest.raises(ValueError): sub.execute_pubsub('psubscribe', Channel('chan:1', is_pattern=False, loop=loop)) with pytest.raises(ValueError): sub.execute_pubsub('punsubscribe', Channel('chan:1', is_pattern=False, loop=loop))
async def consume_from_channel(self, channel: aioredis.Channel): try: # print(f"consume from channel : {channel.name}") # print(f" is active : {channel.is_active}") try: msg = await asyncio.wait_for(channel.get(), timeout=0.1) return msg except : pass except Exception as e: logging.error(stackprinter.format(e, style="darkbg2"))
async def consume_from_channel(self, channel: aioredis.Channel): try: # some data channels might not send any data for some period # if we don't time out we may block the loop try: msg = await asyncio.wait_for(channel.get(), timeout=0.01) await handle_msg(msg) except: pass except Exception as e: logging.error(stackprinter.format(e, style="darkbg2"))
async def _receive_on_channel(self, channel: Channel): log.debug("({cls}) running message receive for '%s'", channel.name, extra=self._fmtargs) async for topic, message in channel.iter(): log.debug("({cls}) received message in topic channel %s", topic, extra=self._fmtargs) try: parsed_message = pickle.loads(message, encoding="utf-8") await self._dispatch_processor(parsed_message) except Exception: log.exception( "failed to parse message and dispatch its processor, continuing" )
async def chat(request, websocket: WebSocketCommonProtocol): # TODO Validate first message from websocket is the channel name channel_name = await websocket.recv() channel_name = ujson.loads(channel_name) channel = Channel(channel_name, is_pattern=False) consumer_handler = await ConsumerHandler.initialize(channel) producer_handler = await ProducerHandler.initialize(channel) consumer_task = asyncio.ensure_future(consumer_handler.handle(websocket)) producer_task = asyncio.ensure_future( producer_handler.broadcast(websocket)) done, pending = await asyncio.wait( [consumer_task, producer_task], return_when=asyncio.FIRST_COMPLETED, ) for task in pending: task.cancel()
async def web_socket_chat(_, websocket: WebSocketCommonProtocol): CONNECTED.add(websocket) channel_name = await websocket.recv() channel_data = json.loads(channel_name) print('channel_data', channel_data) channel = Channel(channel_data['room_id'], is_pattern=False) consumer_handler = await ConsumerHandler.initialize( channel, queue_conn_pub) producer_handler = await ProducerHandler.initialize( channel, queue_conn_sub) consumer_task = asyncio.ensure_future(consumer_handler.handle(websocket)) producer_task = asyncio.ensure_future( producer_handler.broadcast(websocket)) done, pending = await asyncio.wait( [consumer_task, producer_task], return_when=asyncio.FIRST_COMPLETED, ) for task in pending: task.cancel()
async def subscribe_to_redis(path): conn = await create_connection((os.getenv('REDIS_HOST', 'localhost'), 6379)) channel = Channel('{}'.format(path), is_pattern=False) await conn.execute_pubsub('subscribe', channel) return channel, conn
async def subscribe_to_redis(path): conn = await create_connection(('localhost', 6379)) channel = Channel('lightlevel{}'.format(path), is_pattern=False) await conn.execute_pubsub('subscribe', channel) return channel, conn
async def reader(channel: aioredis.Channel): async for msg in channel.iter(): print(f'msg: {msg.decode()}') await socket_manager.broadcast(None, msg.decode())
async def subscribe(channel: str, redis: Redis): (channel_subscription, ) = await redis.subscribe( channel=Channel(channel, False)) while await channel_subscription.wait_message(): yield {"event": "message", "data": await channel_subscription.get()}
async def reader(channel: Channel, socket: WebSocket) -> None: async for message in channel.iter(): await socket.send_text(message.decode('utf-8'))
async def subscribe_to_redis(): conn = await create_connection((REDIS_URL, 6379)) # Set up a subscribe channel channel = Channel('test_websocket', is_pattern=False) await conn.execute_pubsub('subscribe', channel) return channel, conn