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?
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
If the ProcessPoolExecutor does not resolve the problem then the switches between the Python threads should not be the problem.
本文探讨了Python多线程应用中遇到的Spinlock问题,并提供了代码示例。通过对问题的分析,建议使用进程而非线程来避免Spinlock,并推荐了multiprocessing模块和concurrent.futures库作为解决方案。
583

被折叠的 条评论
为什么被折叠?



