In CPython,the global interpreter lock,or GIL,is a mutex that prevents multiple native threads from executing Python bytecodes at once. This lock is necessary mainly because CPython’s memory management is not thread-safe. (However,since the GIL exists,other features have grown to depend on the guarantees that it enforces.)
GIL: 全局解释锁
cpython解释器:
python在执行的时候同一时刻只允许一个线程运行
因为存在GIL,所以cpython中无法利用多核,解决方法是多进程+协程
CPU任务:
IO密集型
计算密集型
time.sleep() 等同于IO操作
对于IO密集型的任务:python的多线程的是有意义的
对于计算密集型的任务:python的多线程不建议使用,python不适用。
多进程+协程实际解决是IO密集型
这篇文章透彻的剖析了GIL对python多线程的影响,强烈推荐看一下:http://www.dabeaz.com/python/UnderstandingGIL.pdf