在 python 中使用多线程真的会产生开销(GIL)吗?
Does using multiple threads in python really produce overhead(GIL)?
来自python wiki:
However the GIL can degrade performance even when it is not a bottleneck. Summarizing those slides: The system call overhead is significant, especially on multicore hardware. Two threads calling a function may take twice as much time as a single thread calling the function twice. The GIL can cause I/O-bound threads to be scheduled ahead of CPU-bound threads. And it prevents signals from being delivered.
我先在单线程中尝试了 运行 一个简单的函数,然后将其与使用 5 个线程进行比较:
from threading import Thread
import time
def count(n):
while n > 0:
n -= 1
a=time.time()
count(100000000)
count(100000000)
count(100000000)
count(100000000)
count(100000000)
print(time.time()-a)
a = time.time()
t1 = Thread(target=count, args=(100000000,))
t1.start()
t2 = Thread(target=count, args=(100000000,))
t2.start()
t3 = Thread(target=count, args=(100000000,))
t3.start()
t4 = Thread(target=count, args=(100000000,))
t4.start()
t5 = Thread(target=count, args=(100000000,))
t5.start()
t1.join()
t2.join()
t3.join()
t4.join()
t5.join()
print(time.time()-a)
我尝试了不同数量的线程,每次多线程版本 运行(稍微)更快。我在 windows 10 机器(64 位)运行 上使用 python 3.7.3(64 位),在英特尔 i5 4 核处理器(8 个逻辑内核)上。
我真的才刚刚开始学习线程,一开始就卡住了,这让人非常沮丧。我还在谷歌搜索发现的其他一些文章中发现了同样的不一致之处,这些文章使用了基本相同的示例代码。我想我的问题是,是否有人可以提供更合适的示例或 link 以进行更清晰的研究。
Does using multiple threads in python really produce overhead(GIL)?
是的,这与您的实验结果一致。我不清楚为什么你认为 Python 维基——一个精心策划的来源——可能在骗你。
I also found the same inconsistancies in some other articles found by googling which used basically the same example code.
我没有看到你声称的不一致之处。你说你的多线程代码 运行 只比你的单线程代码快 一点点 ,但在理想情况下,五线程计算会 运行 五分之一 相同计算的单线程版本的时间。这不是边际差异。
I am really just starting to learn about threading and it's very frustrating to be stuck right at the beginning.
我不清楚你为什么认为自己被卡住了。您成功地 运行 进行了多线程计算,并且显然甚至看到了一点加速。但是如果你的意思是你希望能够观察到更多的加速,那么 Python 不是你的理想平台。您 可以 在 Python 中看到多线程的良好加速,但这在很大程度上取决于工作负载的细节。
确实,您引用的问题描述甚至不是问题的核心。从多线程 Python 代码中获得良好加速的最大挑战不是它,而是它所指的瓶颈问题。 GIL 是一把双刃剑。它保护您免受多线程编程的许多复杂性(因此您不会通过 Python 研究多线程来了解这些),但为了足够通用,它对 [=27] 的实际并发性施加了重大限制=]线程可以实现。
来自python wiki:
However the GIL can degrade performance even when it is not a bottleneck. Summarizing those slides: The system call overhead is significant, especially on multicore hardware. Two threads calling a function may take twice as much time as a single thread calling the function twice. The GIL can cause I/O-bound threads to be scheduled ahead of CPU-bound threads. And it prevents signals from being delivered.
我先在单线程中尝试了 运行 一个简单的函数,然后将其与使用 5 个线程进行比较:
from threading import Thread
import time
def count(n):
while n > 0:
n -= 1
a=time.time()
count(100000000)
count(100000000)
count(100000000)
count(100000000)
count(100000000)
print(time.time()-a)
a = time.time()
t1 = Thread(target=count, args=(100000000,))
t1.start()
t2 = Thread(target=count, args=(100000000,))
t2.start()
t3 = Thread(target=count, args=(100000000,))
t3.start()
t4 = Thread(target=count, args=(100000000,))
t4.start()
t5 = Thread(target=count, args=(100000000,))
t5.start()
t1.join()
t2.join()
t3.join()
t4.join()
t5.join()
print(time.time()-a)
我尝试了不同数量的线程,每次多线程版本 运行(稍微)更快。我在 windows 10 机器(64 位)运行 上使用 python 3.7.3(64 位),在英特尔 i5 4 核处理器(8 个逻辑内核)上。
我真的才刚刚开始学习线程,一开始就卡住了,这让人非常沮丧。我还在谷歌搜索发现的其他一些文章中发现了同样的不一致之处,这些文章使用了基本相同的示例代码。我想我的问题是,是否有人可以提供更合适的示例或 link 以进行更清晰的研究。
Does using multiple threads in python really produce overhead(GIL)?
是的,这与您的实验结果一致。我不清楚为什么你认为 Python 维基——一个精心策划的来源——可能在骗你。
I also found the same inconsistancies in some other articles found by googling which used basically the same example code.
我没有看到你声称的不一致之处。你说你的多线程代码 运行 只比你的单线程代码快 一点点 ,但在理想情况下,五线程计算会 运行 五分之一 相同计算的单线程版本的时间。这不是边际差异。
I am really just starting to learn about threading and it's very frustrating to be stuck right at the beginning.
我不清楚你为什么认为自己被卡住了。您成功地 运行 进行了多线程计算,并且显然甚至看到了一点加速。但是如果你的意思是你希望能够观察到更多的加速,那么 Python 不是你的理想平台。您 可以 在 Python 中看到多线程的良好加速,但这在很大程度上取决于工作负载的细节。
确实,您引用的问题描述甚至不是问题的核心。从多线程 Python 代码中获得良好加速的最大挑战不是它,而是它所指的瓶颈问题。 GIL 是一把双刃剑。它保护您免受多线程编程的许多复杂性(因此您不会通过 Python 研究多线程来了解这些),但为了足够通用,它对 [=27] 的实际并发性施加了重大限制=]线程可以实现。