Python Round Robin 算法和实现

Python 真牛逼,什么库都有。需要写个 round robin 算法,随手一搜,居然也有这么个库,还挺好用,在此分享。如果有需要用 Python 实现 round robin 的,都不用自己写了,直接用这个库即可。Round robin 是一个非常简单又实用的调度机制,也就是所谓的轮询制。

一、安装

安装:

pip install roundrobin

二、使用

>>> import roundrobin
>>> get_roundrobin = roundrobin.basic(["A", "B", "C"])
>>> ''.join([get_roundrobin() for _ in range(7)])
'ABCABCA'
>>> # weighted round-robin balancing algorithm as seen in LVS
>>> get_weighted = roundrobin.weighted([("A", 5), ("B", 1), ("C", 1)])
>>> ''.join([get_weighted() for _ in range(7)])
'AAAAABC'
>>> # smooth weighted round-robin balancing algorithm as seen in Nginx
>>> get_weighted_smooth = roundrobin.smooth([("A", 5), ("B", 1), ("C", 1)])
>>> ''.join([get_weighted_smooth() for _ in range(7)])
'AABACAA'

详见:https://pypi.org/project/roundrobin/


【AD】美国洛杉矶CN2 VPS/香港CN2 VPS/日本CN2 VPS推荐,延迟低、稳定性高、免费备份_搬瓦工vps

【AD】RackNerd 推出的 KVM VPS 特价优惠,在纽约、西雅图、圣何塞和阿什本每年仅需 12.88 美元!