import threading
class MakeThreading(threading.Thread):
def __init__(self, target, threads_list, set_daemon=True):
super().__init__()
self._target = target
self.setDaemon(set_daemon)
self.start()
threads_list.append(self)
if __name__ == '__main__':
from time import sleep
threads = []
def f1():
while True:
print('T1', end=' ')
sleep(1)
def f2():
while True:
print('T2', end=' ')
sleep(1.2)
def f3():
while True:
print('T3')
sleep(1.3)
MakeThreading(f1, threads)
MakeThreading(f2, threads)
MakeThreading(f3, threads)
for T in threads: # 如果主程序没有主循环,就用这个指令,有主循环可以不用
T.join()
while True:
sleep(1)
threading范例以及自定义class
最新推荐文章于 2024-07-09 23:56:48 发布