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

问题描述 投票:0回答:1

我有一个特定的请求,想调用两个函数f1和f2。我想为总请求的95%调用函数f1,为其余5%请求调用f2。我该怎么做?

python random python-requests probability
1个回答
0
投票

使用随机模块

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
© www.soinside.com 2019 - 2024. All rights reserved.