from threading import Thread
class ThreadWithResult(Thread):
def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None):
def function():
self.result = target(*args, **kwargs)
super().__init__(group=group, target=function, name=name, daemon=daemon)
def sum_(x, y):
time.sleep(2)
return x + y
def mul_(x, y):
time.sleep(2)
print(x*y)
x = 5
y = 4
t1 = ThreadWithResult(target=sum_, args=(x,y))
t2 = Thread(target=mul_, args=(x,y))
t1.start()
t2.start()
t1.join()
t2.join()
print(t1.result)