Python自定义装饰器不适用于Celery任务

Cap*_*net 3 python decorator celery python-decorators

我在使用Python装饰器时遇到了麻烦。我有一个这样的场景设置:

def decorated(func):
    def call_and_update(*args, **kwargs):
        # do some stuff here
        output = func(*args, **kwargs)
        # do more stuff here
        return output
    return call_and_update

@celery.task
@decorated
def testA():
    return "Test A"

@celery.task
@decorated
def testB():
    return "Test B"
Run Code Online (Sandbox Code Playgroud)

出于某种原因,我首先调用的那个函数似乎都func保留在装饰器中。

例如,如果我启动一个shell并运行:

>>> testA()
Test A
>>> testB()
Test A
Run Code Online (Sandbox Code Playgroud)

或者,如果我重新启动外壳程序并从第二个测试开始:

>>> testB()
Test B
>>>> testA()
Test B
Run Code Online (Sandbox Code Playgroud)

我发现了与此问题类似的问题,但很少有答案围绕着将扩展类用于任务方法。

如果我特别想通过装饰器和函数来执行此操作,是否有技巧使其起作用?

需要注意的是没有@celery.task装饰,功能工作正常。特别是两个装饰器的组合会导致问题。

谢谢!

小智 5

每个任务都需要有一个唯一的名称celery docs,因为未提供它使用的是包装函数的名称。

@celery.task(name='test-A')
@decorated
def testA():
    return 'test A'

@celery.task(name='test-B')
@decorated
def testB():
    return 'test B'
Run Code Online (Sandbox Code Playgroud)

  • @CaptainPlanet:您也可以在装饰功能中使用[`functools.wraps`](https://docs.python.org/3/library/functools.html#functools.wraps)。通过更新其名称,可以使装饰后的功能看起来像原始功能。 (3认同)