Exponential backoff: time.sleep with random.randint(0, 1000) / 1000

前端 未结 2 984
没有蜡笔的小新
没有蜡笔的小新 2021-01-01 12:12

In many google api\'s code samples i have seen this line of code.

time.sleep((2 ** n) + (random.randint(0, 1000) / 1000))

random.rand

2条回答
  •  灰色年华
    2021-01-01 12:39

    Having a bit of randomness in situations like this is good. For example, if you have a large number of clients hitting the same server, having them use the same deterministic backoff could result in them hitting the server in perfect lockstep, which isn't desirable.

提交回复
热议问题