01、GIL:全局解释器锁【global interpreter lock】
--解释:python有一个机制保证
--01、一个逻辑cpu上只有一个线程在运行[ 即执行编译之后的字节码 ]
--02、无法将多个线程部署到多个cpu上去
--cpython中存在全局解释锁,但是对于pypi解释器就不存在这种问题
--全局解释器释放锁的依据:
--01、根据时间片或字节码执行进行划分 [ python内部决定的 ]
--02、io调用的时候会释放全局解释器锁,因此python多线程设计io操作时速度非常快
--查看程序运行的字节码:dis模块包
import dis
def add(a):
a = a+1
return a
print(dis.dis(add))
--通过定义全局变量 自加函数 自减函数,发现线程不是始终占据资源,在执行过程中线程会释放资源
import threading
total = 0
def add():
global total
for i in range(1000000):
total = total + 1
def desc():
global total
for i in range(1000000):
total = total - 1
thread_01 = threading.Thread(target=add)
thread_02 = threading.Thread(target=desc)
thread_01.start()
thread_02.start()
thread_01.join()
thread_02.join()
print(total)
****** 执行结果 ******
可能为 0~2000000 之间任意一个值,例如1356812
02、多线程编程基本使用,直接定义函数,通过threading.Thread模块进行调用线程:start join setDaemon
--线程:是系统操作执行的最小调度单元
--进程:是系统资源分配的最小调度单元
--对于io操作,多线程 多进程差别不大,甚至多线程由于切换时不需要分配消耗资源和时间,导致其速度快于多进程
--多线程定义案例一:多线程爬虫 + threading模块
--直接通过 threading 模块进行定义执行,这里以爬虫为例:
--爬虫由于是属于socket编程,所以肯定是io编程。因此使用多线程可以达到较高的并发量,实现案例如下:
******线程启动时只有 start 没有 join 和 setDaemon******
--导致线程中主线程不会等待子线程结束之后才执行,而是直接结束运行,所以这里没有等待时间间隔程序运行时间基本为0s
--这种执行方式还有一个特点:在主线程结束后,子线程还是会继续执行完毕
import threading
import time
def get_detail_html_01(url_01):
print("get detail html_01 start")
time.sleep(2)
print(url_01)
print("get detail html_01 and")
def get_detail_html_02(url_02):
print("get detail html_02 start")
time.sleep(2)
print(url_02)
print("get detail html_02 and")
if __name__ == '__main__':
# 定义线程对象,threading.Thread()进行初始化
# threading.Thread()其中参数target为定义的多线程执行的函数名【函数名不可带括号】,参数args和线程函数的参数保持一致
thread_01 = threading.Thread(target=get_detail_html_01, args=("liming_01_yue", ))
thread_02 = threading.Thread(target=get_detail_html_02, args=("liming_02_yue",))
# 定义启动时间
start_time = time.time()
# 启动线程
thread_01.start()
thread_02.start()
# 定义结束时间
print("结束时间为:%s"%(str(time.time() - start_time)))
********************** 输出结果 ************************
get detail html_01 start
get detail html_02 start
结束时间为:0.0006616115570068359
liming_01_yue
get detail html_01 and
liming_02_yue
get detail html_02 and
******线程启动时有 start 和 setDaemon 没有 join******
--这种方式设置setDaemon=True之后,主线程结束之后子线程也会结束
--这种设置方式必须在 start 函数之前进行设置
--daemon:守护进程、半神半精灵
if __name__ == '__main__':
# 定义线程对象,threading.Thread()进行初始化
# threading.Thread()其中参数target为定义的多线程执行的函数名【函数名不可带括号】,参数args和线程函数的参数保持一致
thread_01 = threading.Thread(target=get_detail_html_01, args=("liming_01_yue", ))
thread_02 = threading.Thread(target=get_detail_html_02, args=("liming_02_yue",))
# 定义启动时间
start_time = time.time()
# 启动线程
thread_01.setDaemon(True)
thread_02.setDaemon(True)
thread_01.start()
thread_02.start()
# 定义结束时间
print("结束时间为:%s"%(str(time.time() - start_time)))
********************** 输出结果 ************************
get detail html_01 start
get detail html_02 start
结束时间为:0.0009925365447998047
******线程启动时有 start 和 setDaemon 还有 join******
--这种方式不仅设置setDaemon=True,而且线程执行以后还设置了join阻塞线程。因此主线程会在子线程执行完毕后才结束整个程序
if __name__ == '__main__':
# 定义线程对象,threading.Thread()进行初始化
# threading.Thread()其中参数target为定义的多线程执行的函数名【函数名不可带括号】,参数args和线程函数的参数保持一致
thread_01 = threading.Thread(target=get_detail_html_01, args=("liming_01_yue", ))
thread_02 = threading.Thread(target=get_detail_html_02, args=("liming_02_yue",))
# 定义启动时间
start_time = time.time()
# 启动线程
thread_01.setDaemon(True)
thread_02.setDaemon(True)
thread_01.start()
thread_02.start()
# 阻塞线程
thread_01.join()
thread_02.join()
# 定义结束时间
print("结束时间为:%s"%(str(time.time() - start_time)))
********************** 输出结果 ************************
get detail html_01 start
get detail html_02 start
liming_01_yue
get detail html_01 and
liming_02_yue
get detail html_02 and
结束时间为:2.0041568279266357
03、通过继承Thread类调用多线程:
--通过继承重载threading.Thread模块中的run方法,实现多线程
--具体实现代码如下:
import threading
import time
class GetDetailHtml01(threading.Thread):
def __init__(self, name):
super().__init__(name=name)
def run(self):
print("get detail html_01 start")
time.sleep(2)
print("get detail html_01 and")
class GetDetailHtml02(threading.Thread):
def __init__(self, name):
super().__init__(name=name)
def run(self):
print("get detail html_02 start")
time.sleep(2)
print("get detail html_02 and")
if __name__ == '__main__':
# 初始时间
start_time = time.time()
# 初始化线程
thread01 = GetDetailHtml01('name01')
thread02 = GetDetailHtml02('name02')
# 启动线程
thread01.start()
thread02.start()
# 阻塞线程
thread01.join()
thread02.join()
# 线程结束时间
print("线程结束时间为:%s"%(str(time.time() - start_time)))