一、多线程
线程-程序最小的执行单位
进程-一对资源的集合,图片、文字请求,进程中包含线程
线程的讲解、线程模块threading、线程池模块threadpool
a.每个进程里面默认有一个线程在运行,这个线程称为主线程
b.线程和线程之间是独立的,循环启用线程时是多个线程同时执行,不是挨个执行
1、线程的启用及传参
(1)启用一个线程
import threading
import time
def create_data(url):
time.sleep(3)
print("url",url)
#一个线程的启用,及传参,如果是一个参数时,参数后面需要加逗号
#请用线程方法:先实例化一个线程,并传递该线程需要处理的函数及参数,然后启用这个线程
t = threading.Thread(target=create_data,args=(1,)) #只写函数名,一个参数时,参数后面需要加逗号
t.start()
(2)启用多个线程
import threading
import time
def create_data(url):
time.sleep(3)
print("url",url)
#采用循环启用多个线程,
for i in range(5):
t = threading.Thread(target=create_data,args=(i,))
t.start()
(3)查看程序中启用线程数量
每个进程里面默认有一个线程在运行,这个线程称为主线程
线程和线程之间是独立的
print(threading.activeCount())
(4)统计多线程执行时间,需采用join()函数
方法一:采用等待每个线程的方法统计多线程执行结束时间,将所有启动的线程放在list中,挨个去等待
import threading
import time,random
def create_data(url):
t = random.randint(1,5)
print(t)
time.sleep(t)
print("url",url)
# 统计线程执行时间的方法一:采用等待每个线程的方法统计多线程执行结束时间,将所有启动的线程放在list中,挨个去等待
#采用循环启用多个线程,
start_time = time.time()
threads = []
for i in range(5):
t = threading.Thread(target=create_data,args=(i,))
t.start()
threads.append(t)
for t in threads:
t.join() #等待子线程执行结束
end_time = time.time()
# print(end_time)
print("执行时间",end_time - start_time)
方法二:统计线程剩余个数
import threading
import time,random
def create_data(url):
t = random.randint(1,5)
print(t)
time.sleep(t)
print("url",url)
start_time = time.time()
for i in range(5):
t = threading.Thread(target=create_data,args=(i,))
t.start()
while threading.activeCount() !=1:
pass
end_time = time.time()
# print(end_time)
print("执行时间",end_time - start_time)
2、多线程-锁
多个线程同时操作同一个变量时,可能会出现错误,此时需要加锁,操作前加锁,操作后释放掉
实例化锁:lock = threading.Lock()
加锁方法一:
加锁:lock.acquire()
释放锁:lock.release()
加锁方法二:(不需要释放锁):
with lock:
import threading
count = 0
lock = threading.Lock()
def add_data():
print("当前线程是", threading.currentThread())
global count
#lock.acquire()
with lock:
count+=1
#lock.release()
for i in range(5):
t = threading.Thread(target=add_data)
t.start()
while threading.activeCount() !=1:
pass
print(count)
三、多线程练习-获取qq头像
import requests,threading
import os,time
image_url = "https://q4.qlogo.cn/g?b=qq&nk=%s&s=140"
qq_numbers = ["511402865","260303838","1099276647","1490768397","729226162",
"1164019076","569380213","923296994","330547222","609924484","1054114694",
"854795345"]
def down_load_pic(qq):
url = image_url % qq
req = requests.get(url)
path = os.path.join("images",qq+'.jpg')
with open(path,'wb') as fw:
fw.write(req.content)
start_time = time.time()
print(start_time)
# 单线程
for qq in qq_numbers:
down_load_pic(qq)
# 多线程
# for qq in qq_numbers:
# t = threading.Thread(target=down_load_pic,args=(qq,))
# t.start()
#
# while threading.activeCount() !=1:
# pass
print("总耗时:",time.time() - start_time)
print("所有头像下载完成!")
四、使用线程池模块threadpool
threadpool.makeRequests(down_load_pic,qq_numbers)
分配数据,此处传参任务名称,qq列表
如果需要传递多个参数,??????
import requests,threading
import os,time,random
import threadpool
image_url = "https://q4.qlogo.cn/g?b=qq&nk=%s&s=140"
qq_numbers = ["511402865","260303838","1099276647","1490768397","729226162",
"1164019076","569380213","923296994","330547222","609924484","1054114694",
"854795345"]
def down_load_pic(qq):
url = image_url % qq
req = requests.get(url)
path = os.path.join("images",qq+'.jpg')
with open(path,'wb') as fw:
fw.write(req.content)
# 启用线程池
pool = threadpool.ThreadPool(10) #启用10个线程
reqs = threadpool.makeRequests(down_load_pic,qq_numbers)#分配数据,此处传参是个list
for req in reqs: #把分配好的数据,放到线程池里面
pool.putRequest(req)
pool.wait() #开始运行
五、守护线程
如果把子线程设置成守护线程,主线程干完之后,子线程直接杀死
import threading
import time
def ganhuo():
time.sleep(10)
print("done!")
for i in range(10):
t = threading.Thread(target=ganhuo)
t.setDaemon(True) #把子线程设置成守护线程
t.start()
print("主线程活干完了")
二、多进程
所有的任务分类
1.IO 密集型任务 ==> CPU主要负责任务调度,多线程
2.CPU 密集型任务 ==> 需要复杂计算,多核CPU同时工作计算速度加快,多进程