参考文献:
LVS集群的负载调度
轮叫调度(Round-Robin Scheduling)
import time def rrs(n, i): '''轮叫调度算法流程(Python)''' s = {} # 字典形式定义一组服务器,key为服务器名,value为其权值 for a in range(n): # 初始化服务器集群 s[a] = 1 # 默认权值均为1 j = i while True: j = (j + 1) % n if s[j] > 0: i = j return i if j == i: break def main(): n = int(raw_input("How many servers you have? ")) i = n - 1 # 初始化i为一组服务器的最后一台服务器 while True: print 'Last time: %s' % i i = rrs(n, i) print 'This time: %s' % i print '*************' time.sleep(2) main()
Result:
[stephen@web01 python]$ python rrs2.py How many servers you have? 6 # 定义共有6台服务器{0, 1, 2, 3, 4, 5} Last time: 5 # 上次选择的服务器被初始化为集群服务器中的最后一台--5 This time: 0 # 显示下次将会选择的服务器--0 ************* Last time: 0 This time: 1 ************* Last time: 1 This time: 2 ************* Last time: 2 This time: 3 ************* Last time: 3 This time: 4 ************* Last time: 4 This time: 5 ************* Last time: 5 This time: 0 *************