当函数作为对象在进程中传递时,Decorator不起作用

2024-09-29 21:41:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

@retry(stop_max_attempt_number=2)
def a_func:
  do_somthing

def a_func_thread:
  process = multiprocessing.Process(target=a_func, args=[])
  process.start()

我看到的是,当我直接调用一个函数时,decorator就工作了。但是,当我将它用作进程中的目标函数时,进程似乎根本不尊重decorator。我是不是错过了一些很简单的东西


Tags: 函数代码number进程defdecoratorprocessdo
1条回答
网友
1楼 · 发布于 2024-09-29 21:41:38

Decorator将始终工作,因为Decorator仅在定义函数时被调用,并且Decorator调用的结果随后存储为函数名

def decorator(fnc):
    def test():
        print "test"
    return test

@decorator
def foo():
    print "foo"

foo() # will print test

target=a_func处,a_func是调用@retry decorator的结果

相关问题 更多 >

    热门问题