python演示如何将动态参数传递给装饰器

python演示如何将动态参数传递给装饰器,python,flask,redis,python-decorators,Python,Flask,Redis,Python Decorators,我正在使用python框架。我写了一个需要一个参数的装饰器,这个参数是动态的 我的装饰程序如下所示,将获得一个密钥,并使用该密钥从redis获取数据 def redis_hash_shop_style(key): def fn_wrapper(f): @wraps(f) def decorated_function(*args, **kwargs): data = redis_hash(key) return

我正在使用python框架。我写了一个需要一个参数的装饰器,这个参数是动态的

我的装饰程序如下所示,将获得一个密钥,并使用该密钥从redis获取数据

def redis_hash_shop_style(key):
    def fn_wrapper(f):
        @wraps(f)
        def decorated_function(*args, **kwargs):
            data = redis_hash(key)
            return data
        return decorated_function
return fn_wrapper
我有一个类来使用这个装饰器,代码如下

class ShopAreaAndStyleListAPI(Resource):
    @redis_hash_shop_style(key='shop_{}_style'.format(g.city.id))
    def get(self):
        # if not found from redis, query from mysql
        pass
如您所见,我的decorator需要一个名为
key
的参数,我这样传递密钥

@redis\u hash\u shop\u style(key='shop\u{}\u style'。格式(g.city.id))
g.city.id
将获取城市id,如果一切正常,键将如下

class ShopAreaAndStyleListAPI(Resource):
    @redis_hash_shop_style(key='shop_{}_style'.format(g.city.id))
    def get(self):
        # if not found from redis, query from mysql
        pass
shop_100_风格

但我得到了一个错误:

class ShopAreaAndStyleListAPI(Resource):
File "xx.py", line 659, in ShopAreaAndStyleListAPI

@redis_hash_shop_style(key='shop_{}_style'.format(g.city.id))

File "/Users/xx/.virtualenvs/yy/lib/python2.7/site-packages/werkzeug/local.py", line 347, in __getattr__
return getattr(self._get_current_object(), name)
File "/Users/xx/.virtualenvs/yy/lib/python2.7/site-packages/werkzeug/local.py", line 306, in _get_current_object
return self.__local()
File "/Users/xx/.virtualenvs/yy/lib/python2.7/site-packages/flask/globals.py", line 44, in _lookup_app_object
raise RuntimeError(_app_ctx_err_msg)
RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that 
needed to interface with the current application object in a way.  
To solve this set up an application context with app.app_context().  
See the documentation for more information.
我很困惑,在flask中,如何将动态参数传递给装饰师

谢谢。

如果我们检查文件中的,
flask.g
,它会显示:

要共享仅对从一个函数到另一个函数的一个请求有效的数据,全局变量不够好,因为它会在线程环境中中断。Flask为您提供了一个特殊对象,该对象确保它仅对活动请求有效,并且将为每个请求返回不同的值

这是通过使用线程本地代理(在中)实现的:

我们应该记住的另一件事是,Python在“编译”阶段在任何请求或
flask
应用程序之外执行装饰程序的第一个过程。这意味着
key
参数在
flask
请求上下文之外,当应用程序启动时(当您的类被解析/修饰时),会被分配一个
'shop\u{}\ u style'.format(g.city.id)

但是,我们可以通过使用惰性代理轻松延迟对
flask.g
的访问,该代理仅在使用时通过回调函数获取值。让我们使用已经与
flask
捆绑在一起的:

一般来说(对于非
flask
或非
werkzeug
应用程序),我们可以从软件包中使用类似的
LazyProxy

与此无关,您还需要修复您的
redis\u hash\u shop\u样式
装饰器,以便不仅从
redis
获取,而且在过时(或不存在)的情况下更新(或创建)值,在适当的时候调用wrapped
f()

from werkzeug.local import LocalProxy

class ShopAreaAndStyleListAPI(Resource):
    @redis_hash_shop_style(key=LocalProxy(lambda: 'shop_{}_style'.format(g.city.id)))
    def get(self):
        # if not found from redis, query from mysql
        pass