python线程锁解决方案,如何使用多线程python解决自旋锁问题?

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值