Python:装饰调度员:第 22 条军规

Python: Decorator Dispatcher: Catch-22

我正在尝试构建一个基于装饰器的调度程序,例如 Flask 或 Pyramid 使用的。我得到了一些有用的东西,但是 运行 有点像第 22 条军规。以下代码有效,但这只是因为 foo() 被执行并设置了 .mq_path-attribute。当启动应用程序并构建可调度函数列表时,尚未设置任何属性。我想执行由事件驱动的 foo()。

我可以"manually"提前准备一个函数列表并在添加函数时更新,但我喜欢 Flask 的工作方式,只需向处理 URL 的函数添加装饰器(或者在本例中为 MQ 路径)。

list_of_paths = []
path_dispatcher = {}

def handle_mq(path):
    def decorator(fn):
        def decorated(*args,**kwargs):
            decorated.mq_path = path
            print "Hello from the handle_mq() decorator, your path is: {0}".format(path)
            return fn(*args,**kwargs)
        return decorated
    return decorator

@handle_mq('/some/path')
def foo():
    print "foo!"

foo() # <- this code only works if I first call the decorated function

for k, v in globals().items():
    if hasattr(v, 'mq_path'):
        list_of_paths.append(v.mq_path)
        path_dispatcher[v.mq_path] = v

print list_of_paths
print path_dispatcher
path_dispatcher['/some/path']()

所以基本上问题是,如何在首次执行之前收集修饰函数的列表?

我在 Python 2.7.

我找到答案了!

list_of_paths = []
path_dispatcher = {}

def handle_mq(path):
    def decorator(fn):
        list_of_paths.append(path)
        path_dispatcher[path] = fn
        def decorated(*args,**kwargs):
            print "Hello from handl_mq decorator, your path is: {0}".format(path)
            return fn(*args,**kwargs)
        return decorated
    return decorator

@handle_mq('/some/path')
def foo():
    print "foo!"

print list_of_paths
print path_dispatcher
path_dispatcher['/some/path']()