如何用Python实现配置热加载?
文章目录
什么是配置热加载?
所谓的配置热加载,也就是说当服务收到配置更新消息之后,我们不用重启服务就可以使用最新的配置去执行任务。
为什么要做?
热加载的好处就是不用人工去重启服务,就能配置更新。代码自动去加载新的配置进行使用。
使用Python如何实现?
经过多次实验,尝试了信号量,事件通知,管道,消息队列,等方式。
最后实验得出还是事件通知+消息队列 好使,简单易懂的方式实现了配置热加载的功能。
代码实现细节
schedule 函数主要功能职责:
- 从消息队列中获取值
- 设置事件,使is_set 为 True
- 在无限循环中去运行一个常驻任务,当is_set被设置为False时,退出循环。
def schedule():
while True:
print('wait message...')
case_configurations = scheduler_notify_queue.get()
print(f"Got case configurations {case_configurations=}...")
task_schedule_event.set() # 设置set之后, is_set 为True
print(f"Schedule will start ...")
while task_schedule_event.is_set(): # is_set 为True的话,那么任务就会一直执行
run(case_configurations)
print("Clearing all scheduling job ...")
event_schedule 函数主要功能职责:
- 将配置存放到消息队列中
- clear 所有的事件通知,将is_set 设置为False
def event_schedule(case_config):
scheduler_notify_queue.put(case_config)
print(f"Put cases config to the Queue ...")
task_schedule_event.clear() # clear之后,is_set 为False
print(f"Clear scheduler jobs ...")
print(f"Schedule new job ...")完整代码实现如下:
import multiprocessing
import time
scheduler_notify_queue = multiprocessing.Queue() # 创建一个队列
task_schedule_event = multiprocessing.Event() # 创建一个事件通知
def run(case_configurations: str):
print(f'{case_configurations} running...')
time.sleep(3)
def schedule():
while True:
print('wait message...')
case_configurations = scheduler_notify_queue.get()
print(f"Got case configurations {case_configurations=}...")
task_schedule_event.set()
print(f"Schedule will start ...")
while task_schedule_event.is_set():
run(case_configurations)
print("Clearing all scheduling job ...")
def event_schedule(case_config: str):
scheduler_notify_queue.put(case_config)
print(f"Put cases config to the Queue ...")
task_schedule_event.clear()
print(f"Clear scheduler jobs ...")
print(f"Schedule job ...")
def main():
scheduler_notify_queue.put('1') # 首先存放一个值到队列中
p = multiprocessing.Process(target=schedule) # 使用多进程调度schdule
p.start()
count = 1
while True:
if count == 5: # 模拟等待了5秒之后,配置更新了
event_schedule('100') # 更换配置
if count == 10: # 模拟等待了5秒之后,配置更新了
event_schedule('200') # 更换配置
count += 1
time.sleep(1)
if __name__ == '__main__':
main()运行结果如下:
wait message...
Got case configurations case_configurations='1'...
Schedule will start ...
1 running...
1 running...
Put cases config to the Queue ...
Clear scheduler jobs ...
Schedule job ...
Clearing all scheduling job ...
wait message...
Got case configurations case_configurations='100'...
Schedule will start ...
100 running...
100 running...
Put cases config to the Queue ...
Clear scheduler jobs ...
Schedule job ...
Clearing all scheduling job ...
wait message...
Got case configurations case_configurations='200'...
Schedule will start ...
200 running...
200 running...
200 running...
200 running...
200 running...
200 running...END ~
看完了,点个赞呗 👇 (你的鼓励,是我更新的动力💪)!
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论