← 返回题库
中级

实现发布订阅模式

未完成
中级参考 代码结构已给出,请填写 ____ 处
def solve():
    from pyodide.http import open_url
    from io import StringIO
    messages_csv = open_url(____).read()
    import pandas as pd, json
    from io import StringIO
    msgs = pd.read_csv(StringIO(____))
    class EventBus:
        def __init__(____):
            self.subscribers = {}
            self.delivered = []
        def subscribe(____):
            self.subscribers.setdefault(____).append(____)
        def publish(____):
            for handler in self.subscribers.get(____):
                try:
                    handler(____)
                    self.delivered.append((____))
                except Exception as e:
                    self.delivered.append((____))
    bus = EventBus()
    notifications = []
    inventory_ops = []
    bus.subscribe('____', lambda t, p: inventory_ops.append(____))
    bus.subscribe('____', lambda t, p: notifications.append(____))
    bus.subscribe('____', lambda t, p: notifications.append(____))
    bus.subscribe('____', lambda t, p: notifications.append(____))
    for _, row in msgs.head(____).iterrows():
        try:
            payload = json.loads(____)
            bus.publish(____)
        except: pass
    print(____)
    print(____)
    print(____)

示例

输入
solve()
期望输出
inventory_ops: []
notifications: ['notify_paid:ORD004', 'notify_pay:ORD007', 'notify_paid:ORD010']
总投递: 3, ok=3
Python 代码 🔒 登录后使用
🔒

登录后即可练习

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