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