前言
呜呜呜毕设好难,学点好玩的东西娱乐一下吧…就选了python多线程。
例子
from threading import Thread
class Model:
def __init__(self):
self.lt = [2021]
def add(self, num):
self.lt.append(num)
# 创建 Thread 的子类
class MyThread(Thread):
def __init__(self, model):
'''
:param func: 可调用的对象
:param args: 可调用对象的参数
'''
Thread.__init__(self)
self.model = model
def run(self):
self.model.add(555)
def run_thread_func(m):
# 创建 Thread 实例
t = MyThread(m)
# 启动线程运行
t.start()
# 等待所有线程执行完毕
t.join()
if __name__ == '__main__':
m = Model()
run_thread_func(m)
print(m.lt)