版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41185868/article/details/85226940
Python之多线程:python多线程设计之同时执行多个函数命令详细攻略
目的
同时执行多个函数命令
采取方法
T1、单个实现
import threading
threading.Thread(target=my_record()).start()
threading.Thread(target= voice_tts()).start()
import threading
processing2=threading.Thread(target= voice_tts())
processing1=threading.Thread(target=my_record())
processing1.start()
processing2.start()
processing1.join()
processing2.join()
T2、列表for循环实现
#实现多线程
threads = []
t1 = threading.Thread(target=my_record())
threads.append(t1)
t2 = threading.Thread(target=voice_tts())
threads.append(t2)
for t in threads:
t.setDaemon(True)
t.start()
t.join()
print("ok")
应用场景
1、多个按钮函数内调用一个主类,继承threading.Thread
import threading
class MyThread01(threading.Thread):
def __init__(self,parent=None):
threading.Thread.__init__(self)
def run(self):
print('线程开启!')
#按钮101
def test_run():
time.sleep(2)
testthread = MyThread01(test_run)
testthread.start()
voice_tts()
#按钮102
def test_run():
time.sleep(2)
testthread = MyThread01(test_run)
testthread.start()
my_record()