简体   繁体   中英

Decorator does not work when function is passed as object in a process

I have the following code:

@retry(stop_max_attempt_number=2)
def a_func:
  do_somthing

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

What I am seeing is that the decorator works when I call a_func directly. But when I use it as a target function in a process, the process does not seem to respect decorators at all. Am I missing something very simple here?

Decorator will always work, because decorator is only called when function is defined, and the result of decorator call is then stored as the function name.

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

@decorator
def foo():
    print "foo"

foo() # will print test

At the point of target=a_func , in a_func is the result of calling the @retry decorator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM