简单的发布订阅模式代码:
#!/usr/bin/env python # -*- coding: utf-8 -*- # time @2016/3/16 11:30 create by JasonYang from collections import defaultdict container = defaultdict(list) def sub(topic, callback): if callback in container[topic]: return container[topic].append(callback) def pub(topic, *args, **kwargs): for func in container[topic]: func(*args, **kwargs) def greeting(name): print "hello %s" % name sub('greet', greeting) pub('greet', 'dashu')