python使用函数创建多线程(一)
学会使用函数创建多线程
Python提供了一个内置模块threading.Thread,可以很简单的创建多线程,多线程也是串行的。
多线程示例
import time
import threading
#自定义线程函数
def main(name='python'):
for i in range(2):
print('hello',name)
time.sleep(1)
if __name__ == "__main__":
#创建线程t1,不指定参数
t1 = threading.Thread(target = main)
t1.start()
#创建线程t2,指定参数
t2 = threading.Thread(target = main,args = ("yangyukuan",))
t2.start()
输出结果:
hello python
hello yangyukuan
hello python
hello yangyukuan