声明:参考B站视频,自学成长记录
https://www.bilibili.com/video/BV1zy4y1M7xt?p=1
GIL
'''
> GIL(cpython)
gil相当于在cpu上加了一把锁
在同一时刻只有一个线程在cpu上执行字节码
无法发挥多核cpu得优势
> gil释放机制
根据执行得字节码行数
根据执行时间
遇到IO操作
'''
多线程
'''
> 多线程
threading.Thread(target=fn, args=(x,))
对于IO操作来说, 多线程和多进程性能差别不大
'''
import time
import threading
def login():
print('login start {}'.format(threading.currentThread().ident))
time.sleep(1)
print('login end {}'.format(threading.currentThread().ident))
def index():
print('index start {}'.format(threading.currentThread().ident))
time.sleep(1)
print('index end {}'.format(threading.currentThread().ident))
if __name__ == '__main__':
# 模式1 多线程启动
print('main start {}'.format(threading.currentThread().ident))
login = threading.Thread(target=login)
index = threading.Thread(target=index)
# 启动线程
login.start()
index.start()
print('mian end {}'.format(threading.currentThread().ident))
'''
执行结果:
main start 7288
login start 11956
index start 1048
mian end 7288
index end 1048
login end 11956
'''
多线程 - 守护线程
import time
import threading
def login():
print('login start {}'.format(threading.currentThread().ident))
time.sleep(1)
print('login end {}'.format(threading.currentThread().ident))
def index():
print('index start {}'.format(threading.currentThread().ident))
time.sleep(1)
print('index end {}'.format(threading.currentThread().ident))
if __name__ == '__main__':
# 模式2 守护线程
print('main start {}'.format(threading.currentThread().ident))
login = threading.Thread(target=login)
index = threading.Thread(target=index)
# 守护线程 - 主消亡子就消亡
login.setDaemon(True)
index.setDaemon(True)
# 启动线程
login.start()
index.start()
print('mian end {}'.format(threading.currentThread().ident))
'''
执行结果:
main start 3932
login start 2036
index start 7644
mian end 3932
'''
多线程 - 阻塞
import time
import threading
def login():
print('login start {}'.format(threading.currentThread().ident))
time.sleep(1)
print('login end {}'.format(threading.currentThread().ident))
def index():
print('index start {}'.format(threading.currentThread().ident))
time.sleep(1)
print('index end {}'.format(threading.currentThread().ident))
if __name__ == '__main__':
# 模式3 阻塞
print('main start {}'.format(threading.currentThread().ident))
login = threading.Thread(target=login)
index = threading.Thread(target=index)
# 启动线程
login.start()
index.start()
# 阻塞 主线程等子线程结束后再结束
login.join()
index.join()
print('mian end {}'.format(threading.currentThread().ident))
'''
执行结果:
main start 13476
login start 11364
index start 12884
login end 11364
index end 12884
mian end 13476
'''
多线程 - 继承Thread类方式
import time
import threading
class login(threading.Thread):
def __init__(self, name):
super().__init__(name=name)
def run(self):
print('login start {}'.format(threading.currentThread().ident))
time.sleep(1)
print('login end {}'.format(threading.currentThread().ident))
class index(threading.Thread):
def __init__(self, name):
super().__init__(name=name)
def run(self):
print('index start {}'.format(threading.currentThread().ident))
time.sleep(1)
print('index end {}'.format(threading.currentThread().ident))
if __name__ == '__main__':
# 模式4 继承Thread类
print('main start {}'.format(threading.currentThread().ident))
login = login('login')
index = index('index')
# 启动线程
login.start()
index.start()
# 阻塞
login.join()
index.join()
print('mian end {}'.format(threading.currentThread().ident))
'''
执行结果:
main start 15220
login start 11316
index start 3116
login end 11316
index end 3116
mian end 15220
'''
线程间通信
import time
import threading
from queue import Queue
def get_detail_url(queue):
# 爬取url列表
# while True:
print("get detail url started")
time.sleep(1)
for i in range(10):
# 已满时 会阻塞
queue.put('http://www.baidu.com/{}'.format(i))
print("get detail url end!")
def get_detail_html(queue):
# 获取文章详情
# while True:
# 为空时 会阻塞
print("get detail html started")
url = queue.get()
print("get detail html end! {}".format(url))
if __name__ == '__main__':
# 实例化Queue 并设置最大队列长度为1000
queue = Queue(maxsize=1000)
# 1个生产者(同时生产10个)
url = threading.Thread(target=get_detail_url, args=(queue,))
url.start()
# 3个消费者
for i in range(3):
html = threading.Thread(target=get_detail_html, args=(queue,))
html.start()
# 队列阻塞主线程
queue.join()
'''
执行结果:
get detail url started
get detail html started
get detail html started
get detail html started
get detail url end!
get detail html end! http://www.baidu.com/0
get detail html end! http://www.baidu.com/1
get detail html end! http://www.baidu.com/2
'''