赞
踩
1.简单的线程
同时运行下面的sing和dance函数
- import time
- import threading
-
- def sing():
- for i in range(2):
- print("唱歌...")
- time.sleep(1)
-
-
- def dance():
- for i in range(2):
- print("跳舞...")
- time.sleep(1)
-
- if __name__ == '__main__':
- sing_process = threading.Thread(target=sing)
- dance_process = threading.Thread(target=dance)
-
- sing_process.start()
- dance_process.start()
结果如下:
二 、带有参数的线程
- import time
- import threading
-
- def sing(num):
- for i in range(num):
- print("唱歌...")
- time.sleep(1)
-
-
- def dance(num):
- for i in range(num):
- print("跳舞...")
- time.sleep(1)
-
- if __name__ == '__main__':
- sing_process = threading.Thread(target=sing,args=(2,))
- dance_process = threading.Thread(target=dance,kwargs={"num":2})
-
- sing_process.start()
- dance_process.start()
一样的结果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。