Python常用的多线程:
_thread(Python2.X是thread),面向过程
threading,比上者更高级,面向对象
学习_thread(),其实非常简单,使用start_new_thread()开启一个新线程即可,代码如下:
# ——创建时间:2019.3.2——
# 多线程之基本_thread
import random
import _thread as thread
from time import sleep
# 线程函数
def fun(a,b):
print(a,b)
# 随机休眠
sleep(random.randint(1,5))
# 启动8个线程
for i in range(8):
thread.start_new_thread(fun,(i+1,'a'*(i+1)))
# 任意输入一个字符暂停程序
input()
注意:start_new_thread()的第二个参数必须是元组!