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/

赞(0)
未经允许不得转载:老唐笔记 » Python Round Robin 算法和实现