Python在dict上实现Round Robin

时间:2018-11-24 20:57:39

标签: python round-robin

我正在尝试创建一个客户端服务器应用程序,客户端在其中注册请求,并在以后获得响应。

对于快速插入,我使用defaultdict

{
    "john":   [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
    "ram":    [2, 6],
    "bruce":  [1, 4, 5],
    "willam": [7, 1],
}

此数据结构唯一容易受到的问题是"john"发出过多的请求,并且服务器无法及时为其他客户端提供服务。

所以我认为roundrobin可能会救出来,给我一个迭代器,该迭代器可以产生这样的客户-

"john", 0
"ram", 2
"bruce", 1
"willam", 7
"john", 1
"ram", 6
"bruce", 4
... 

任何人都可以告诉我如何高效地实现这样的迭代器吗?

编辑:这是我想出的。有没有人有更好的做事方法?

def roundrobin(requests): 
    remaining = set(requests) 

    index = 0 
    while remaining: 
        up_next = set() 
        for key in remaining: 
            try: 
                print(key, requests[key][index])
            except IndexError: 
                continue 
            up_next.add(key) 
        remaining = up_next 
        index += 1 

它产生以下输出

ram 2
john 0
willam 7
bruce 1
bruce 4
ram 6
john 1
willam 1
john 2
bruce 5
john 3
john 4
john 5
john 6
john 7
john 8
john 9
john 10
john 11
john 12
john 13
john 14
john 15
john 16
john 17
john 18
john 19

2 个答案:

答案 0 :(得分:1)

我认为没有什么比这更好的了。

def roundrobin2(requests):
    index = 0
    while requests:
        for key in list(requests):
            try:
                key, requests[key][index]
            except IndexError:
                del requests[key]
            else:
                index += 1

答案 1 :(得分:1)

您可以为每个请求者创建一个存储桶,并以itertools.cycle循环,每次弹出。

import itertools

all_requests = {
    "john":   [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
    "ram":    [2, 6],
    "bruce":  [1, 4, 5],
    "willam": [7, 1],
}

# handle requests:
for requester in itertools.cycle(all_requests):
    request, all_requests[requester] = all_requests[requester][0], all_requests[requester][1:]
    # Intuitively this seems faster than request = all_requests[requester].pop(0), but I could be wrong
    # you should profile this before using it in production code, or see my note below.

    response = handle_request(request)
    send_response(response)

请注意,我经常从此列表的最前面开始,因此您应该使用collections.deque来代替,它具有快速弹出和从头或尾推动的功能。