博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Python创建线程

Posted on 2023-08-07 10:42  steve.z  阅读(12)  评论(0编辑  收藏  举报
import threading,time


def fn1(x, y, z):
	while True:
		print("I'm doing sport.")
		print('args: %s %s %s' % (x, y, z))
		time.sleep(1)

def fn2(a, b, c):
	while True:
		print("I'm singing.")
		time.sleep(1)



if __name__ == '__main__':
	# 创建一个线程
	th1 = threading.Thread(target = fn1, kwargs = {"x": "hello", "y": "welcome", "z": "to"})
	th1.start()

	# 创建另一个线程
	th2 = threading.Thread(None, fn2, args = ('1', '2', '3'))
	th2.start()