我正在用Python编写多线程应用程序,并遇到了内核时间飙升的问题。使用perf
我发现它确实是一个自旋锁:
54.89% python [kernel.kallsyms] [k] __ticket_spin_lock
导致自旋锁的代码如下:
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
答案 0 :(得分:0)
我真的不知道这是哪个螺旋锁,但你启动了多少螺纹?
如何不启动线程而是进程?
查看多处理或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线程之间的切换应该不是问题。