2022-02-01 18:20:14 +08:00
|
|
|
|
|
2022-01-27 21:21:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import threading
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NewThread(threading.Thread):
|
2022-02-01 18:20:14 +08:00
|
|
|
|
'''新建一个进程来运行函数,函数运行完毕后可以使用.getResult方法获取其返回值'''
|
2022-01-27 21:21:25 +08:00
|
|
|
|
def __init__(self, func, args=()):
|
|
|
|
|
super(NewThread, self).__init__()
|
|
|
|
|
self.func = func
|
|
|
|
|
self.args = args
|
2022-02-02 15:09:11 +08:00
|
|
|
|
self.result = None
|
|
|
|
|
|
2022-01-27 21:21:25 +08:00
|
|
|
|
def run(self):
|
2022-02-01 18:20:14 +08:00
|
|
|
|
self.result = self.func(*self.args)
|
2022-02-02 15:09:11 +08:00
|
|
|
|
|
2022-01-27 21:21:25 +08:00
|
|
|
|
def getResult(self):
|
|
|
|
|
threading.Thread.join(self) # 等待线程执行完毕
|
2022-02-02 15:09:11 +08:00
|
|
|
|
return self.result
|
2022-01-27 21:21:25 +08:00
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# ————————————————
|
|
|
|
|
# 版权声明:上面的类NewThread修改自CSDN博主「星火燎愿」的原创文章中的内容,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
|
|
|
|
|
# 原文链接:https://blog.csdn.net/xpt211314/article/details/109543014
|
|
|
|
|
# ————————————————
|
2022-02-01 18:20:14 +08:00
|
|
|
|
#
|