Dynadot Jtti 搬瓦工 腾讯云

Python Round Robin 算法和实现

GigsGigsCloud

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/

Dynadot Hostwinds
赞(0)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《Python Round Robin 算法和实现
文章链接:https://oldtang.com/2336.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。