小编典典

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

redis

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

我的装饰器如下所示,将获得一个密钥,并使用该密钥从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

如您所见,我的装饰器需要一个名为的参数key,然后像这样传递密钥

@redis_hash_shop_style(key='shop_{}_style'.format(g.city.id)) g.city.id
将获得城市的ID,如果一切正常,密钥将如下所示

shop_100_style

但我得到了错误:

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.

我很困惑,在烧瓶中,如何将动态参数传递给装饰器?

谢谢。


阅读 1038

收藏
2020-06-20

共1个答案

小编典典

如果我们检查的文档flask应用全球flask.g它说:

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

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

g = LocalProxy(partial(_lookup_app_object, 'g'))

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

但是我们可以flask.g通过使用延迟代理轻松地延迟访问,该代理仅通过回调函数来获取值。让我们使用已与捆绑在一起flaskwerkzeug.local.LocalProxy

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

一般来说(对于非flask或非werkzeug应用),我们可以使用一个类似的LazyProxyProxyTypes包。

与此无关,您还希望将redis_hash_shop_style装饰器固定为不仅可以从获取redis,还可以通过f()在适当时调用wrapd来更新(或创建)陈旧(或不存在)的值。

2020-06-20