29 lines
1.3 KiB
Python
29 lines
1.3 KiB
Python
import json
|
||
import redis.asyncio as redis
|
||
|
||
class RedisClient:
|
||
def __init__(self, host: str, port: int, task_channel: str, result_channel: str, text_task_channel: str = "text_task_channel"):
|
||
self.client = redis.Redis(host=host, port=port, decode_responses=True)
|
||
self.task_channel = task_channel
|
||
self.result_channel = result_channel
|
||
self.text_task_channel = text_task_channel
|
||
|
||
async def get_task(self, timeout=0):
|
||
"""Получает задачу из очереди с помощью BLPOP"""
|
||
result = await self.client.blpop(self.task_channel, timeout=timeout)
|
||
if result:
|
||
_, task_json = result
|
||
try:
|
||
return json.loads(task_json)
|
||
except Exception as e:
|
||
print(f"Error parsing task message: {e}")
|
||
return None
|
||
|
||
async def publish_result(self, result: dict):
|
||
"""Публикует результат в канал результатов"""
|
||
await self.client.publish(self.result_channel, json.dumps(result))
|
||
|
||
async def send_to_summarize(self, task_data: dict):
|
||
"""Отправляет текст в сервис суммаризации"""
|
||
await self.client.rpush(self.text_task_channel, json.dumps(task_data))
|