This repository has been archived on 2024-10-27. You can view files and clone it, but cannot push or open issues or pull requests.
local-llm-server/llm_server/cluster/redis_cycle.py

36 lines
1.0 KiB
Python
Raw Normal View History

2023-09-29 00:09:44 -06:00
import redis
2023-09-30 19:41:50 -06:00
redis_cycler_db = redis.Redis(host='localhost', port=6379, db=9)
2023-09-29 00:09:44 -06:00
def redis_cycle(list_name):
2023-09-30 19:41:50 -06:00
"""
Emulates itertools.cycle() but returns the complete shuffled list.
:param list_name:
:return:
"""
to_move = redis_cycler_db.rpop(list_name)
if not to_move:
return []
redis_cycler_db.lpush(list_name, to_move)
new_list = redis_cycler_db.lrange(list_name, 0, -1)
return [x.decode('utf-8') for x in new_list]
def add_backend_cycler(list_name: str, new_elements: list):
existing_elements = [i.decode('utf-8') for i in redis_cycler_db.lrange(list_name, 0, -1)]
existing_set = set(existing_elements)
with redis_cycler_db.pipeline() as pipe:
# Add elements
for element in new_elements:
if element not in existing_set:
pipe.rpush(list_name, element)
# Remove elements
for element in existing_set:
if element not in new_elements:
pipe.lrem(list_name, 0, element)
pipe.execute()