关于redis批量获取数据pipeline

2020-03-23  本文已影响0人  葡萄柚子茶

Redis批量获取数据

Redis是建立在TCP协议上的CS架构,客户端client对redis server采取请求响应的方式交互.每次交互会有网络延迟,大约30ms.
假设有这样一个场景,redis中存储上千个key值,获取每个key对应field的value,那么要向redis请求上千次 hget(key, field),获取响应也是对应的次数.如果能一次性将所有请求提交给server端,执行完成后批量获取响应,只需向redis请求1次,性能获大幅提升

conn = connect_redis()
    keys = collect_keys()
    pipe = conn.pipeline()
    for k in keys:
        pipe.hmget(k, 'bo', 'so')
    result_data = pipe.execute()

没有用pipeline之前,基本上获取所有数据需要90多s,现在只需0.3s,性能提升清晰可见

APscheduler 注意事项

实现以下场景:定时任务每隔1s执行任务函数,但是任务函数执行完成的时间比1s要长,此时启动定时任务要加上两个参数,否则会报错

import time
from apscheduler.schedulers.blocking import BlockingScheduler


scheduler = BlockingScheduler()
def job1():
    time.sleep(3)
    current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    print(current_time)

scheduler.add_job(job1, 'interval', max_instances=10, misfire_grace_time=3600, seconds=1)
scheduler.start()

max_instances

可允许的实例个数,如果没有设置,则默认为1,表示id相同的任务实例数
像上面的例子中,会报skipped: maximum number of running instances reached (1)的错误,意思APScheduler试图重新执行作业,但前一个仍在运行。

misfire_grace_time

这个参数可以理解为任务的超时容错配置,给executor 一个超时时间,这个时间范围内要是该跑的还没跑完,就别再跑了
像上面的例子中,会报Run time of job …… next run at: ……)” was missed by的错误

上一篇下一篇

猜你喜欢

热点阅读