Musicreater/msctspt/threadOpera.py

32 lines
1.1 KiB
Python
Raw Normal View History

2022-01-19 05:32:53 +00:00
# 诸葛亮与八卦阵帮忙修改语法 日期:---2022年1月19日
# 统计致命三级错误0个警告二级错误0个语法一级错误9个--未解决1个
2021-11-20 16:59:15 +00:00
import threading
class NewThread(threading.Thread):
2022-01-19 05:32:53 +00:00
"""新建一个进程来运行函数,函数运行完毕后可以使用.getResult方法获取其返回值"""
2021-11-20 16:59:15 +00:00
def __init__(self, func, args=()):
super(NewThread, self).__init__()
self.func = func
self.args = args
2022-01-19 05:32:53 +00:00
2021-11-20 16:59:15 +00:00
def run(self):
self.result = self.func(*self.args)
2022-01-19 05:32:53 +00:00
2021-11-20 16:59:15 +00:00
def getResult(self):
threading.Thread.join(self) # 等待线程执行完毕
try:
return self.result
2022-01-19 05:32:53 +00:00
except ValueError:
2021-11-20 16:59:15 +00:00
return None
#
# ————————————————
# 版权声明上面的类NewThread修改自CSDN博主「星火燎愿」的原创文章中的内容遵循CC 4.0 BY-SA版权协议转载请附上原文出处链接及本声明。
# 原文链接https://blog.csdn.net/xpt211314/article/details/109543014
# ————————————————
2022-01-19 05:32:53 +00:00
#