我有一个特定的请求,想调用两个函数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