import threading
class MyThread(threading.Thread):
def __init__(self,*args,**kwargs):
threading.Thread.__init__(self,*args,**kwargs)
def run(self) -> None:
try:
if self._target:
self.result=self._target(*self._args, **self._kwargs)
finally:
# Avoid a refcycle if the thread is running a function with
# an argument that has a member that points to the thread.
del self._target, self._args, self._kwargs
def foo(a,b):
return a+b
mt=MyThread(target=foo,args=(1,2))
mt.start()
mt.join()
if hasattr(mt,result):
print(mt.result)
python获取线程返回结果,简单重写Thred类run方法
最新推荐文章于 2023-03-14 14:57:38 发布