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.