通過研究案例,徹底掌握Python GIL
Python因其全局解釋器鎖(GIL)而聲名狼藉。GIL限制了Python解釋器一次只能執(zhí)行一個線程。在現(xiàn)代多核CPU上,這是一個問題,因為程序無法利用多個核心。不過,盡管存在這種限制,Python仍已成為從后端Web應(yīng)用到AI/ML和科學(xué)計算等領(lǐng)域的頂級語言。
1、訓(xùn)練數(shù)據(jù)管道的結(jié)構(gòu)
對于大多數(shù)后端Web應(yīng)用來說,GIL的限制并不是一個約束,因為它們通常受到I/O的限制。在這些應(yīng)用中,大部分時間只是等待來自用戶、數(shù)據(jù)庫或下游服務(wù)的輸入。系統(tǒng)只需具備并發(fā)性,而不一定需要并行性。Python解釋器在執(zhí)行I/O操作時會釋放GIL,因此當(dāng)線程等待I/O完成時,就會給另一個線程獲得GIL并執(zhí)行的機(jī)會。
GIL的限制不會影響大多數(shù)計算密集型的AI/ML和科學(xué)計算工作負(fù)載,因為像NumPy、TensorFlow和PyTorch等流行框架的核心實(shí)際上是用C++實(shí)現(xiàn)的,并且只有Python的API接口。大部分計算可以在不獲取GIL的情況下進(jìn)行。這些框架使用的底層C/C++內(nèi)核庫(如OpenBLAS或Intel MKL)可以利用多個核心而不受GIL的限制。
當(dāng)同時有I/O和計算任務(wù)時會發(fā)生什么?
2、使用純Python的計算任務(wù)
具體來說,可以考慮以下兩個簡單的任務(wù)。
import time
def io_task():
start = time.time()
while True:
time.sleep(1)
wake = time.time()
print(f"woke after: {wake - start}")
start = wake
def count_py(n):
compute_start = time.time()
s = 0
for i in range(n):
s += 1
compute_end = time.time()
print(f"compute time: {compute_end - compute_start}")
return s
在這里,通過休眠一秒鐘來模擬一個I/O限制的任務(wù),然后喚醒并打印它休眠了多長時間,然后再次休眠。count_py
是一個計算密集型的任務(wù),它簡單地對數(shù)字n進(jìn)行計數(shù)。如果同時運(yùn)行這兩個任務(wù)會發(fā)生什么?
import threading
io_thread = threading.Thread(target=io_task, daemnotallow=True)
io_thread.start()
count_py(100000000)
輸出結(jié)果如下:
woke after: 1.0063529014587402
woke after: 1.009704828262329
woke after: 1.0069530010223389
woke after: 1.0066332817077637
compute time: 4.311860084533691
count_py
需要大約4.3秒才能計數(shù)到一百萬。但是io_task
在同一時間內(nèi)運(yùn)行而不受影響,大約在1秒后醒來,與預(yù)期相符。盡管計算任務(wù)需要4.3秒,但Python解釋器可以預(yù)先從運(yùn)行計算任務(wù)的主線程中釋放GIL,并給予io_thread
獲得GIL并運(yùn)行的機(jī)會。
3、使用numpy的計算任務(wù)
現(xiàn)在,本文將在numpy中實(shí)現(xiàn)計數(shù)函數(shù),并進(jìn)行與之前相同的實(shí)驗,但這次要計數(shù)到一千萬,因為numpy的實(shí)現(xiàn)效率更高。
import numpy as np
def count_np(n):
compute_start = time.time()
s = np.ones(n).sum()
compute_end = time.time()
print(f"compute time: {compute_end - compute_start}")
return s
io_thread = threading.Thread(target=io_task, daemnotallow=True)
io_thread.start()
count_np(1000000000)
輸出結(jié)果如下:
woke after: 1.0001161098480225
woke after: 1.0008511543273926
woke after: 1.0004539489746094
woke after: 1.1320469379425049
compute time: 4.1334803104400635
這顯示的結(jié)果與上一次實(shí)驗類似。在這種情況下,不是Python解釋器預(yù)先釋放了GIL,而是numpy自己主動釋放了GIL。
這是否意味著在獨(dú)立的線程中同時運(yùn)行I/O任務(wù)和計算任務(wù)總是安全的?
4、使用自定義C++擴(kuò)展的計算任務(wù)
現(xiàn)在,本文將用Python的C++擴(kuò)展實(shí)現(xiàn)計數(shù)函數(shù)。
// importing Python C API Header
#include <Python.h>
#include <vector>
static PyObject *count(PyObject *self, PyObject *args){
long num;
if (!PyArg_ParseTuple(args, "l", &num))
return NULL;
long result = 0L;
std::vector<long> v(num, 1L);
for (long i=0L; i<num; i++) {
result += v[i];
}
return Py_BuildValue("l", result);
}
// defining our functions like below:
// function_name, function, METH_VARARGS flag, function documents
static PyMethodDef functions[] = {
{"count", count, METH_VARARGS, "Count."},
{NULL, NULL, 0, NULL}
};
// initializing our module informations and settings in this structure
// for more informations, check head part of this file. there are some important links out there.
static struct PyModuleDef countModule = {
PyModuleDef_HEAD_INIT, // head informations for Python C API. It is needed to be first member in this struct !!
"count", // module name
NULL,
-1,
functions // our functions list
};
// runs while initializing and calls module creation function.
PyMODINIT_FUNC PyInit_count(void){
return PyModule_Create(&countModule);
}
可以通過運(yùn)行python setup.py build
來構(gòu)建擴(kuò)展,使用以下setup.py
:
from distutils.core import setup, Extension
count_module = Extension('count', sources=['count.cpp'])
setup(name='python_count_extension',
versinotallow='0.1',
descriptinotallow='An Example For Python C Extensions',
ext_modules=[count_module],
)
然后,使用作為自定義擴(kuò)展實(shí)現(xiàn)的計數(shù)函數(shù)運(yùn)行實(shí)驗:
import count
def count_custom(n):
compute_start = time.time()
s = count.count(n)
compute_end = time.time()
print(f"compute time: {compute_end - compute_start}")
return s
io_thread = threading.Thread(target=io_task, daemnotallow=True)
io_thread.start()
count_custom(1000000000)
得到如下結(jié)果:
woke after: 4.414866924285889
compute time: 4.414893865585327
在這種情況下,計算任務(wù)持有GIL,并阻止I/O線程運(yùn)行。
Python解釋器只能在兩個Python字節(jié)碼指令之間預(yù)先釋放GIL,在擴(kuò)展中,是否自愿釋放GIL取決于擴(kuò)展的實(shí)現(xiàn)。
在這種情況下,本例進(jìn)行了一個不會影響任何Python對象的瑣碎計算,因此可以在C++的計數(shù)函數(shù)中使用宏Py_BEGIN_ALLOW_THREADS
和Py_END_ALLOW_THREADS
來釋放GIL:
static PyObject *count(PyObject *self, PyObject *args){
long num;
if (!PyArg_ParseTuple(args, "l", &num))
return NULL;
long result = 0L;
Py_BEGIN_ALLOW_THREADS
std::vector<long> v(num, 1L);
for (long i=0L; i<num; i++) {
result += v[i];
}
Py_END_ALLOW_THREADS
return Py_BuildValue("l", result);
}
使用這種實(shí)現(xiàn)方式,當(dāng)重新運(yùn)行實(shí)驗時,會得到如下結(jié)果:
woke after: 1.0026037693023682
woke after: 1.003467082977295
woke after: 1.0028629302978516
woke after: 1.1772480010986328
compute time: 4.186192035675049
5、結(jié)論
在使用Python時,了解GIL是很重要的。在大多數(shù)常見情況下,可能不會遇到它的限制。但是,如果使用包裝C/C++庫的第三方Python包(除了標(biāo)準(zhǔn)的NumPy、SciPy、TensorFlow或PyTorch),在涉及到任何重型計算時可能會遇到一些問題。在開發(fā)自定義擴(kuò)展時,最好在進(jìn)行重型計算之前釋放GIL,以便其他Python線程有機(jī)會運(yùn)行。