16

假设我的 Python 应用程序中有一个函数,它定义了某种上下文 -user_id例如。此函数调用不将此上下文作为函数参数的其他函数。例如:

def f1(user, operation):
    user_id = user.id
    # somehow define user_id as a global/context variable for any function call inside this scope
    f2(operation)

def f2(operation):
    # do something, not important, and then call another function
    f3(operation)

def f3(operation):
    # get user_id if there is a variable user_id in the context, get `None` otherwise
    user_id = getcontext("user_id")
    # do something with user_id and operation

我的问题是:

  • Python 3.7的上下文变量可以用于此目的吗?如何?
  • 这就是这些上下文变量的用途吗?
  • 如何使用 Python v3.6 或更早版本执行此操作?

编辑

由于多种原因(架构遗留、库等),我不能/不会更改中间函数的签名f2,所以我不能只user_id作为参数传递,也不能将所有这些函数放在同一个类中。

4

3 回答 3

17

您可以contextvars在 Python 3.7 中使用您所询问的内容。这通常很容易:

import contextvars

user_id = contextvars.ContextVar("user_id")

def f1(user, operation):
    user_id.set(user.id)
    f2()

def f2():
    f3()

def f3():
    print(user_id.get(default=None))  # gets the user_id value, or None if no value is set

上的set方法ContextVar返回一个实例,您可以使用该实例将变量重置为操作发生Token之前的值。set因此,如果您想f1按原样恢复事物(对于user_id上下文变量并不是真正有用,但与在模块中设置精度等更相关decimal),您可以执行以下操作:

token = some_context_var.set(value)
try:
    do_stuff()    # can use value from some_context_var with some_context_var.get()
finally:
    some_context_var.reset(token)

该模块还有更多内容contextvars,但您几乎可以肯定不需要处理其他内容。如果您从头开始编写自己的异步框架,您可能只需要创建自己的上下文并在其他上下文中运行代码。

如果您只是使用现有的框架(或编写一个您想与异步代码配合使用的库),您不需要处理这些东西。只需创建一个全局ContextVar(或查找一个已经由您的框架定义的)及其getset的值,如上所示,您应该一切顺利。

很多contextvars使用可能会在后台进行,作为各种库的实现细节,这些库希望具有“全局”状态,不会泄漏线程之间或单个线程内的单独异步任务之间的更改。在这种情况下,上面的示例可能更有意义:f1并且f3是同一个库的一部分,并且f2是用户提供的回调传递到其他地方的库中。

于 2018-06-14T11:22:48.600 回答
8

本质上,您正在寻找的是一种在一组函数之间共享状态的方法。在面向对象语言中这样做的规范方法是使用一个类:

class Foo(object):
    def __init__(self, operation, user=None):
        self._operation = operation
        self._user_id = user.id if user else None

    def f1(self):
        print("in f1 : {}".format(self._user_id))
        self.f2()

    def f2(self):
        print("in f2 : {}".format(self._user_id))
        self.f3()

    def f3(self):
        print("in f3 : {}".format(self._user_id))


 f = Foo(operation, user)
 f.f1()

使用此解决方案,您的类实例(此处f)是执行函数的“上下文”——每个实例都有自己的专用上下文。

函数式编程等价物是使用闭包,我不打算在这里举个例子,因为虽然 Python 支持闭包,但它仍然是第一个并且主要是一种对象语言,所以 OO 解决方案是最明显的。

dict最后,干净的程序解决方案是在整个调用链中传递这个上下文(可以表示为一个或任何类似的数据类型),如 DFE 的回答所示。

作为一般规则:依赖全局变量或某些“魔术”上下文,这些上下文可以(或不)由 you-dont-who-nor-where-nor-when 设置,这使得代码即使不是不可能也很难推理,这可能会以最不可预测的方式打破(谷歌搜索“全球邪恶”将产生大量关于该主题的文献)。

于 2018-06-14T11:15:55.497 回答
2

您可以在函数调用中使用 kwargs 以通过

def f1(user, operation):
    user_id = user.id
    # somehow define user_id as a global/context variable for any function call inside this scope
    f2(operation, user_id=user_id)

def f2(operation, **kwargs):
    # do something, not important, and then call another function
    f3(operation, **kwargs)

def f3(operation, **kwargs):
    # get user_id if there is a variable user_id in the context, get `None` otherwise
    user_id = kwargs.get("user_id")
    # do something with user_id and operation

kwargsdict 等同于您在上下文变量中查看的内容,但仅限于调用堆栈。它是在每个函数中传递(通过类似指针)的相同内存元素,并且不复制内存中的变量。

在我看来,但我想看看你们都怎么想,上下文变量是一种优雅的方式来授权全局变量并控制它。

于 2018-06-14T10:18:26.097 回答