进程是分配系统资源(CPU时间、内存等)的基本单位。线程CPU调度的基本单位。
0.GIL(全局解释器锁)
首先需要明确的一点是GIL
并不是Python的特性,它是在实现Python解析器(CPython)时所引入的一个概念。像其中的JPython就没有GIL。然而因为CPython是大部分环境下默认的Python执行环境。所以在很多人的概念里CPython就是Python,也就想当然的把GIL
归结为Python语言的缺陷。
GIL导致同一进程里的多个线程在同一时刻只能有一个线程在cpu中运行。
而每个进程有各自独立的GIL,互不干扰,因此在cpython中,多进程的执行效率优于多线程(仅仅针对多核CPU而言)
1.多进程与多线程
多进程:
import multiprocessing
def func():
print('process',multiprocessing.current_process())
processes= []
for _ in range(2):
processes.append(multiprocessing.Process(target=func,args=()))
## 启动进程
for process in processes:
process.start()
多线程:
import threading
def func():
print('thread:',threading.current_thread())
threads = []
for _ in range(5):
threads.append(threading.Thread(target=func,args=()))
## 启动线程
for thread in threads:
thread.start()
注意事项:
真正的多线程:
for thread in threads:
thread.start()
for thread in threads:
thread.join() #阻塞主线程
假的多线程:
for thread in threads:
thread.start()
thread.join()
2.多进程和多线程的区别
维度 | 多进程 | 多线程 | 总结 |
---|---|---|---|
数据共享 | 数据是分开的,共享复杂 | 多线程共享进程数据,共享简单 | 各有优势 |
创建销毁 | 创建销毁速度慢 | 创建销毁速度快 | 线程占优 |
可靠性 | 进程间不会相互影响 | 一个线程挂掉将导致整个进程挂掉 | 进程占优 |
3.线程通信与同步
1.线程通信:共享全局变量(线程不安全)/ Queue(线程安全,from queue import Queue)
2.线程同步:锁(threading.Lock() 一般锁,一个线程中尝试多次获得同一个锁,可能会导致死锁 ,此时应该使用threading.RLock()可重入锁,指一个线程中可多次获得同一个锁 )
多线程使用的是queue里面的Queue,多进程使用的是multiprocessing里面的Queue
4.遇到的坑
1.多进程模块中的Queue,即multiprocessing.Queue
假设:queue=Queue()
queue.put()和queue.get()可信,但queue.empty()不可信,queue.empty()可能会在queue中有数据的情况下返回了一个True。
解决办法:无限循环+queue.get(timeout=1)取不到时break