gpt4 book ai didi

锁定与性能的 Python 成本(多线程有意义吗?)

转载 作者:太空狗 更新时间:2023-10-30 01:46:58 26 4
gpt4 key购买 nike

我正在从事一个项目,其中我的代码的吞吐量非常重要,经过一番考虑后我选择让我的程序线程化。

主线程和子线程都在两个共享字典中添加和删除。考虑到在 python 中锁定的性能,我一直在通过互联网查看一些输入,它是一个缓慢的操作等等。

所以我要说的是,因为 python 实际上根本不是线程化的(想想 GIL 只在一个内核上工作),如果我的应用程序需要高性能,我有什么可以通过使其线程化来赢得的,除了用于处理 IO?

编辑

真正的问题是(在有见地的评论之后)

多线程在 python 中有意义吗,因为有 GIL?

最佳答案

IMO,锁解决方案对性能的影响很大,主要是当多个线程真正等待它时。

获取和释放无竞争 锁的成本应该是微不足道的。

This thread显示了对此的测试。

Ok, here is the cost of acquiring and releasing an uncontended lock under Linux, with Python 3.2:

$ python3 -m timeit \
-s "from threading import Lock; l=Lock(); a=l.acquire; r=l.release" \
"a(); r()"

10000000 loops, best of 3: 0.127 usec per loop

And here is the cost of calling a dummy Python function:

$ python3 -m timeit -s "def a(): pass" "a(); a()"

1000000 loops, best of 3: 0.221 usec per loop

And here is the cost of calling a trivial C function (which returns the False singleton):

$ python3 -m timeit -s "a=bool" "a(); a()"

10000000 loops, best of 3: 0.164 usec per loop

Also, note that using the lock as a context manager is actually slower, not faster as you might imagine:

$ python3 -m timeit -s "from threading import Lock; l=Lock()" \
"with l: pass"

1000000 loops, best of 3: 0.242 usec per loop

At least under Linux, there doesn't seem to be a lot of room for improvement in lock performance, to say the least.

PS: RLock is now as fast as Lock:

$ python3 -m timeit \
-s "from threading import RLock; l=RLock(); a=l.acquire; r=l.release" \
"a(); r()"

10000000 loops, best of 3: 0.114 usec per loop

关于锁定与性能的 Python 成本(多线程有意义吗?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11966471/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com