python使用函数创建多线程(一)

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

猜你喜欢

转载自blog.csdn.net/weixin_39549161/article/details/85652750