午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術(shù)文章
文章詳情頁

python 使用多線程創(chuàng)建一個Buffer緩存器的實現(xiàn)思路

瀏覽:183日期:2022-07-19 08:08:11

這幾天學(xué)習(xí)人臉識別的時候,雖然運行的沒有問題,但我卻意識到了一個問題

在圖片進行傳輸?shù)臅r候,GPU的利用率為0

也就是說,圖片的傳輸速度和GPU的處理速度不能很好銜接

于是,我打算利用多線程開發(fā)一個buffer緩存

實現(xiàn)的思路如下

定義一個Buffer類,再其構(gòu)造函數(shù)中創(chuàng)建一個buffer空間(這里最好使用list類型)

我們還需要的定義線程鎖LOCK(數(shù)據(jù)傳輸和提取的時候會用到)

因為需要兩種方法(讀數(shù)據(jù)和取數(shù)據(jù)),所以我們需要定義兩個鎖

實現(xiàn)的代碼如下:

#-*-coding:utf-8-*-import threading class Buffer: def __init__(self,size): self.size = size self.buffer = [] self.lock = threading.Lock() self.has_data = threading.Condition(self.lock) # small sock depand on big sock self.has_pos = threading.Condition(self.lock) def get_size(self): return self.size def get(self): with self.has_data: while len(self.buffer) == 0:print('I can’t go out has_data')self.has_data.wait()print('I can go out has_data') result = self.buffer[0] del self.buffer[0] self.has_pos.notify_all() return result def put(self, data): with self.has_pos: #print(self.count) while len(self.buffer)>=self.size:print('I can’t go out has_pos')self.has_pos.wait()print('I can go out has_pos') # If the length of data bigger than buffer’s will wait self.buffer.append(data) # some thread is wait data ,so data need release self.has_data.notify_all() if __name__ == '__main__':buffer = Buffer(3)def get(): for _ in range(10000): print(buffer.get()) def put(): a = [[1,2,3,4,5,6,7,8,9],[1,2,3,4,5,6,7,8,9],[1,2,3,4,5,6,7,8,9]] for _ in range(10000): buffer.put(a) th1 = threading.Thread(target=put) th2 = threading.Thread(target=get) th1.start() th2.start() th1.join() th2.join()

python 使用多線程創(chuàng)建一個Buffer緩存器的實現(xiàn)思路

總結(jié)

到此這篇關(guān)于python 使用多線程創(chuàng)建一個Buffer緩存器的文章就介紹到這了,更多相關(guān)python 多線程Buffer緩存器內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 六月丁香久久 | 久久久免费看片 | 日干夜干天天干 | 四方色播 | 日韩欧美在线观看 | 亚洲欧美中文字幕 | 欧美久久影院 | 激情xxxx | 日本特黄视频 | www国产在线 | 亚洲黄色片在线观看 | 欧美视频在线一区 | 在线播放h| 亚洲天堂男人天堂 | 91中文字幕网 | 亚洲一二三区在线 | 国产精品一二三四五区 | 日日夜夜精品视频免费 | 欧美综合区| 美日韩在线观看 | 在线观看第一页 | 性爱一级视频 | 欧美激情影音先锋 | 四虎影院成人 | 殴美一级片 | 四虎免费视频 | 91国产精品一区 | 久久精品99国产国产精 | 久久久久久久免费 | 午夜精品一区二区三区视频 | 日本精品视频在线 | 国产三级第一页 | 在线看av网址 | 在线中文字幕网站 | 成人性视频网站 | 特黄免费| 综合天堂av久久久久久久 | 青春草在线 | 91亚色在线观看 | 国产视频在线观看免费 | 久久爱影视i |