python线程数设置多少_为什么线程数增加超过threading.BoundedSemaphore在python的设置?...

由于@Tim Peters说,在你的主线程,你获得一种信号,创建一个线程,释放信号量,重复此过程200次,从来没有减少信号灯的0

如果你要的号码为了控制创建的线程数,在主线程中,您应该首先获取一个信号量,然后创建一个线程。在线程例程结束时,释放信号而不是由主线程完成。

下面是修改:

#!/usr/bin/env python2.7

#coding: utf-8

import threading

import time

class Test:

#set the max thread number

threadLimiter = threading.BoundedSemaphore(10)

def go(self):

lstRecordThreads = []

for i in range(1, 200):

Test.threadLimiter.acquire()

recordThread = threading.Thread(target=self.recordExec, args=(i,))

recordThread.start()

lstRecordThreads.append(recordThread)

for rt in lstRecordThreads:

rt.join()

def recordExec(self, number):

print "current number=["+str(number)+"]"

time.sleep(1)

print "done=["+str(number)+"]"

print('Active thread count: %d' % (threading.active_count(),))

Test.threadLimiter.release()

if __name__ == '__main__':

t = Test()

t.go()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值