Commit ac110535 authored by ZZH's avatar ZZH

change batch redis hmget to pipeline 2023-6-28

parent 63a143d0
import json import json
import math
import pandas as pd import pandas as pd
import pendulum import pendulum
from pot_libs.settings import SETTING from pot_libs.settings import SETTING
...@@ -559,7 +561,17 @@ async def batch_load_rt_ele(mtids): ...@@ -559,7 +561,17 @@ async def batch_load_rt_ele(mtids):
try: try:
db = SETTING.mysql_db db = SETTING.mysql_db
keys = [f"real_time:electric:{db}:{mtid}" for mtid in mtids] keys = [f"real_time:electric:{db}:{mtid}" for mtid in mtids]
for rt_ele in await RedisUtils().mget(keys):
i_size = 500
lst_rt_ele = []
for i in range(math.ceil(len(mtids) / i_size)):
pipe = await RedisUtils().client.pipeline()
for key in keys[i_size * i:i_size * (i + 1)]:
await pipe.get(key)
lst_rt_ele += await pipe.execute()
for rt_ele in lst_rt_ele:
if rt_ele is None: if rt_ele is None:
continue continue
...@@ -581,8 +593,21 @@ async def batch_load_rt_ele_with_hr(mtids): ...@@ -581,8 +593,21 @@ async def batch_load_rt_ele_with_hr(mtids):
keys = [f"real_time:electric:{db}:{mtid}" for mtid in mtids] keys = [f"real_time:electric:{db}:{mtid}" for mtid in mtids]
key_hrs = [f"real_time:electric_hr:{db}:{mtid}" for mtid in mtids] key_hrs = [f"real_time:electric_hr:{db}:{mtid}" for mtid in mtids]
lst_rt_ele = await RedisUtils().mget(keys) i_size = 500
lst_rt_ele_hr = await RedisUtils().mget(key_hrs) lst_rt_ele, lst_rt_ele_hr = [], []
for i in range(math.ceil(len(mtids) / i_size)):
pipe = await RedisUtils().client.pipeline()
for key in keys[i_size * i:i_size * (i + 1)]:
await pipe.get(key)
lst_rt_ele += await pipe.execute()
pipe = await RedisUtils().client.pipeline()
for key in key_hrs[i_size * i:i_size * (i + 1)]:
await pipe.get(key)
lst_rt_ele_hr += await pipe.execute()
for i, mtid in enumerate(mtids): for i, mtid in enumerate(mtids):
rt_ele, rt_ele_hr = lst_rt_ele[i], lst_rt_ele_hr[i] rt_ele, rt_ele_hr = lst_rt_ele[i], lst_rt_ele_hr[i]
if rt_ele and rt_ele_hr: if rt_ele and rt_ele_hr:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment