← 返回题库
中级

实现配置中心与动态更新

未完成
中级参考 代码结构已给出,请填写 ____ 处
def solve():
    from pyodide.http import open_url
    from io import StringIO
    rate_limit_config_csv = open_url(____).read()
    import pandas as pd
    from io import StringIO
    cfg = pd.read_csv(StringIO(____))
    class ConfigCenter:
        def __init__(____):
            self.config = {}
            self.listeners = {}
        def set(____):
            old = self.config.get(____)
            self.config[key] = value
            for fn in self.listeners.get(____):
                fn(____)
        def get(____):
            return self.config.get(____)
        def watch(____):
            self.listeners.setdefault(____).append(____)
    cc = ConfigCenter()
    changes = []
    def on_change(____): changes.append(____)
    for _, row in cfg.iterrows():
        key = f"____"
        cc.set(key, {'____':int(____),'____':int(____),'____':row['____']})
        cc.watch(____)
    cc.set(____)
    cc.set(____)
    print(____)
    for key, val in sorted(cc.config.items()):
        print(____)
    print(____)

示例

输入
solve()
期望输出
配置中心状态:
  rate_limit:/api/v1/orders: {'max': 20, 'window': 60, 'type': 'fixed_window'}
  rate_limit:/api/v1/payments: {'max': 10, 'window': 60, 'type': 'token_bucket'}
  rate_limit:/api/v1/products: {'max': 50, 'window': 60, 'type': 'fixed_window'}
  rate_limit:/api/v1/search: {'max': 20, 'window': 60, 'type': 'sliding'}
  rate_limit:/api/v1/users: {'max': 30, 'window': 60, 'type': 'leaky'}
变更通知: ["rate_limit:/api/v1/orders: {'max': 10, 'window': 60, 'type': 'fixed_window'}->{'max': 20, 'window': 60, 'type': 'fixed_window'}", "rate_limit:/api/v1/payments: {'max': 5, 'window': 60, 'type': 'token_bucket'}->{'max': 10, 'window': 60, 'type': 'token_bucket'}"]
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码