Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的漏桶_Python_Multithreading_Bucket - Fatal编程技术网

python中的漏桶

python中的漏桶,python,multithreading,bucket,Python,Multithreading,Bucket,嗨,我正在尝试用Python开发一个具有无限存储容量的leakybucket。我希望它是线程安全的,CPU效率高,线程数最少。现在它一般都能工作了。但也有一些微小的错误 我将带宽限制在500 kbps。但第三条线似乎打破了这一点。还有,有人能告诉我这是否是实现leakybucket的正确方法吗?谢谢 收费:500.00 收费:500.00 收费:550.00 收费:500.00 收费:500.00 收费:500.00 收费:500.00 收费:500.00 代码如下: from collecti

嗨,我正在尝试用Python开发一个具有无限存储容量的leakybucket。我希望它是线程安全的,CPU效率高,线程数最少。现在它一般都能工作了。但也有一些微小的错误

我将带宽限制在500 kbps。但第三条线似乎打破了这一点。还有,有人能告诉我这是否是实现leakybucket的正确方法吗?谢谢

收费:500.00 收费:500.00 收费:550.00 收费:500.00 收费:500.00 收费:500.00 收费:500.00 收费:500.00

代码如下:

from collections import deque
import threading, time
class LeakyBucket:
    '''the leaky bucket throttling the bit rate'''

    def __init__(self, node, bitsPerSec, measIntv, LBtype):
        self.node = node
        self.bitsPerSec = bitsPerSec  #the rate limit
        self.measIntv = measIntv      #the measure interval, tokens will become full at the beginning of each interval
        self.LBtype = LBtype          #the type of the bucket  
        self.lastTime = 0             #the start time of the last measure interval
        self.bitsDone = 0             #the bits that have been transmitted
        self.BDLock = threading.Lock() #the lock for the bits sent
        self.packDQ = deque()          #the packet Q 
        self.maxToken = bitsPerSec*float(measIntv)  #the max token (bits)
        self.token = self.maxToken      #the current token
        self.condition = threading.Condition()  #sync lock


    def packIn(self, msg):
        '''Insert a packet'''
        self.condition.acquire()
        self.packDQ.append(msg)
        self.condition.notify()
        self.condition.release()

    def keepPoping(self):
        '''keep poping new pack'''
        self.lastTime = time.time()   #record the start time
        while True:
            timeNow = time.time() 
            if  timeNow - self.lastTime > self.measIntv:     
                #new intv, need to reset token
                self.token = self.maxToken
                self.lastTime = timeNow
            self.condition.acquire()
            if self.packDQ: # the queue is not empty
                pack = list(self.packDQ)[0]
                packLen = len(pack[2])*8
                if  packLen > self.token:   #no enough token?
                    #self.packDQ.popleft()
                    self.condition.release()
                    time.sleep(max(self.lastTime+self.measIntv-time.time(),0)) #wait for enough token
                    else:              #enough token, can send out the packet
                    self.packDQ.popleft()
                    self.condition.release()
                    self.changeBitsDone(packLen)
                    self.token = self.token - packLen #consume token
            else:
                self.condition.wait()
                self.condition.release()

    def begin(self):
        '''begin the leakybucket'''
        aThread = threading.Thread(target = self.keepPoping, args = [])
        aThread.start()


    def getBitsDone(self):
        '''get and reset bitsDone, for testing'''
        self.BDLock.acquire()
        reV = self.bitsDone
        self.bitsDone = 0
        self.BDLock.release()
        return reV

    def changeBitsDone(self,length):
        '''change bitsDone, for testing'''
        self.BDLock.acquire()
        self.bitsDone += length
        self.BDLock.release()

    def measure(self, intv):
        '''measure the throughput of the leaky bucket'''
        while True:
            bitsDone = self.getBitsDone()
            rate = bitsDone / float(intv*1024)
            print 'rate: %.2f' % rate
            time.sleep(intv)

    def startMeasure(self, intv):
        '''start measure the rate'''
        #print 'here'
        aThread = threading.Thread(target = self.measure, args = [intv])
        aThread.start()        

#===============================
def main():
    pack = 1000*'a'
    msg = ('192.168.1.1', 16000, pack)
    print 'here'
    LB = LeakyBucket(None, 500*1024, 1, 'reg')
    LB.begin()
    LB.startMeasure(10)
    numMsg = 0

    while numMsg < 10000:
        LB.packIn(msg)
        #print 'pack in'
        numMsg += 1

if __name__ == '__main__':
    main()
从集合导入数据
导入线程,时间
类漏桶:
''漏桶限制比特率''
定义初始化(self、node、bitsPerSec、measIntv、LBtype):
self.node=节点
self.bitsPerSec=bitsPerSec#速率限制
self.measIntv=measIntv#度量间隔,令牌将在每个间隔开始时变满
self.LBtype=LBtype#铲斗的类型
self.lastTime=0#上次测量间隔的开始时间
self.bitsDone=0#已传输的位
self.BDLock=threading.Lock()#发送位的锁
self.packDQ=deque()#数据包Q
self.maxToken=bitsPerSec*float(measIntv)#最大令牌(位)
self.token=self.maxToken#当前令牌
self.condition=threading.condition()#同步锁
def包装(自我,味精):
''插入数据包''
self.condition.acquire()
self.packDQ.append(msg)
self.condition.notify()
self.condition.release()
def keepPoping(自我):
“不断推出新包装”
self.lastTime=time.time()#记录开始时间
尽管如此:
timeNow=time.time()
如果timeNow-self.lastTime>self.measIntv:
#新建intv,需要重置令牌
self.token=self.maxToken
self.lastTime=timeNow
self.condition.acquire()
如果self.packDQ:#队列不是空的
pack=list(self.packDQ)[0]
packLen=len(pack[2])*8
如果packLen>self.token:#没有足够的token?
#self.packDQ.popleft()
self.condition.release()
time.sleep(max(self.lastTime+self.measIntv time.time(),0))#等待足够的令牌
否则:#足够的令牌,可以发送数据包
self.packDQ.popleft()
self.condition.release()
self.changeBitsDone(packLen)
self.token=self.token-packLen#消费令牌
其他:
self.condition.wait()
self.condition.release()
def begin(自我):
“开始泄密”
aThread=threading.Thread(target=self.keepPoping,args=[])
aThread.start()
def GetBitsOne(自身):
''获取并重置BitsOne,用于测试''
self.BDLock.acquire()
reV=self.bitsDone
self.bitsDone=0
self.BDLock.release()
返回版本
def changeBitsDone(自身,长度):
''更改位单,用于测试''
self.BDLock.acquire()
self.bitsDone+=长度
self.BDLock.release()
def测量(自身、intv):
“测量漏桶的吞吐量”
尽管如此:
bitsDone=self.getBitsDone()
速率=比特数/浮点数(intv*1024)
“打印速率:%.2f%”速率
时间。睡眠(intv)
def开始测量(自身、intv):
“开始测量速率”
#打印“此处”
aThread=threading.Thread(target=self.measure,args=[intv])
aThread.start()
#===============================
def main():
包装=1000*a
味精=('192.168.1.1',16000,每包)
打印“此处”
LB=漏桶(无,500*1024,1,'reg')
b.开始
磅开始测量(10)
numsg=0
当numsg<10000时:
豆荚(味精)
#打印“打包”
numsg+=1
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()

什么是“第三行似乎打破了这一点”?代码的第三行就是
类LeakyBucket:
。示例输出的第三行(或该块表示的任何内容),如果只是一个空行。代码中没有任何东西听起来像是你可以合理地称之为“行”的东西的集合。那么?还有,“打破这个”是什么意思?就像一个书呆子和不相关的旁注:“poping”是“pope”的分词,所以你的功能意味着“继续成为天主教会的领袖”。“pop”的分词是“popping”(或者,如果有电子恐惧音乐播放,“poppin”)。这一点都不重要;教皇弗朗西斯一世(Pope Francis I)在著名的糖果厂(candy factory)插曲中拼命地洗牌Lucille Ball之类的包裹,这一想法让我稍微有点开心。OP现在可能已经消失了,但“第三行”指的是印在下面的第三个数字,大于500。