当函数作为进程中的对象传递时,装饰器不起作用
Decorator does not work when function is passed as object in a process
我有以下代码:
@retry(stop_max_attempt_number=2)
def a_func:
do_somthing
def a_func_thread:
process = multiprocessing.Process(target=a_func, args=[])
process.start()
我看到的是,当我直接调用 a_func 时装饰器起作用了。但是当我将它用作进程中的目标函数时,该进程似乎根本不尊重装饰器。我在这里错过了一些非常简单的东西吗?
装饰器将始终有效,因为装饰器仅在定义函数时调用,装饰器调用的结果然后存储为函数名。
def decorator(fnc):
def test():
print "test"
return test
@decorator
def foo():
print "foo"
foo() # will print test
在target=a_func
处,在a_func
是调用@retry装饰器的结果
我有以下代码:
@retry(stop_max_attempt_number=2)
def a_func:
do_somthing
def a_func_thread:
process = multiprocessing.Process(target=a_func, args=[])
process.start()
我看到的是,当我直接调用 a_func 时装饰器起作用了。但是当我将它用作进程中的目标函数时,该进程似乎根本不尊重装饰器。我在这里错过了一些非常简单的东西吗?
装饰器将始终有效,因为装饰器仅在定义函数时调用,装饰器调用的结果然后存储为函数名。
def decorator(fnc):
def test():
print "test"
return test
@decorator
def foo():
print "foo"
foo() # will print test
在target=a_func
处,在a_func
是调用@retry装饰器的结果