Threading with Decorator in Python

Viewed 179

I am in search of a way to threading with wrapper, yet, the result of wrapped function MUST be still available.

Via searching Stackoverflow, a not so perfect solution appreared. As the code below:

def thread_run(func):
    """
    :param func:
    :return:
    """
    def wrapper(*args, **kwargs):
        s = threading.Thread(target=func, args=args, kwargs=kwargs)
        s.daemon = True
        s.start()
        return s
    return wrapper

with this wrapper do not suffice, because:

@thread_run
def powers(number):
    return number**2


if __name__ in "__main__":
   print(powers(2))  #out put <Thread(Thread-1, stopped daemon 123145406771200)>

I want the output of snippet to be 4 print(powers(2))

what should i do?

0 Answers
Related