-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
24 lines (20 loc) · 871 Bytes
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from infer import BatchInference
from utils import pack_req
import aioredis
class BatchInferenceClient:
async def __aenter__(self):
self.redis = await aioredis.create_redis_pool('redis://localhost')
return self
async def __aexit__(self, exc_type, exc, tb):
self.redis.close()
await self.redis.wait_closed()
async def infer(self, req_id, text):
response_key = f'{BatchInference.reponse_key}:{req_id}'
ch = await self.redis.subscribe(response_key)
packed_binary = pack_req(req_id, text)
await self.redis.rpush(BatchInference.request_key, packed_binary)
response = await ch[0].get_json()
await self.redis.unsubscribe(response_key)
assert(req_id == response.get("req_id"))
assert(text == response.get("response_text"))
return response.get("reponse_audio")