如何针对收到的总请求的不同百分比调用两个函数?

How can I call two functions for different percentage of the total requests received?

我有一个特殊的要求,想调用两个函数 f1 和 f2。我想为总请求的 95% 调用函数 f1,为剩余的 5% 请求调用函数 f2。我怎样才能做到这一点?

使用随机模块

import random


def request_handler():
    test = random.random() # random number in [0, 1)
    if test < 0.95:        # 95% of the time, the number will be < 0.95
        return f1()        
    return f2()            # rest of the time, it'll be higher