如何解决多线程python的自旋锁问题? [英] How to resolve spinlock issues with multithreaded python?
本文介绍了如何解决多线程python的自旋锁问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用Python编写一个多线程应用程序,遇到了内核时间激增的问题.使用perf
,我看到它确实是自旋锁:
I'm writing a multithreaded application in Python and have came across an issue where kernel time skyrockets. Using perf
I see that it is indeed a spinlock:
54.89% python [kernel.kallsyms] [k] __ticket_spin_lock
导致自旋锁的代码如下:
The code that causes the spinlock is below:
for i in range(0, my_uuids_len):
while threading.active_count() >= concurrency:
time.sleep(0.1)
threading.Thread(target=read_benchmark,args=(read_times,my_uuids.pop(),)).start()
counter += 1
推荐答案
我真的不知道这是哪个自旋锁,但是您要启动多少个线程?
I really do not know which spinlock this is but how many threads do you start?
不是启动线程而是启动进程?
How about not starting threads but processes?
看看多处理或Python 3.
have a look at multiprocessing or in Python 3.
from concurrent.futures import ThreadPoolExecutor # should do exactly what your code does now
from concurrent.futures import ProcessPoolExecutor # I recommend that
如果ProcessPoolExecutor
不能解决问题,那么Python线程之间的切换应该不是问题.
If the ProcessPoolExecutor
does not resolve the problem then the switches between the Python threads should not be the problem.
这篇关于如何解决多线程python的自旋锁问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文