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

您的位置:首頁技術文章
文章詳情頁

python 使用多線程創建一個Buffer緩存器的實現思路

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

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

在圖片進行傳輸的時候,GPU的利用率為0

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

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

實現的思路如下

定義一個Buffer類,再其構造函數中創建一個buffer空間(這里最好使用list類型)

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

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

實現的代碼如下:

#-*-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 使用多線程創建一個Buffer緩存器的實現思路

總結

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

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 国产一区不卡视频 | 国产91对白在线播放 | 日韩一二三四 | 性插动态图 | 91啪国产| 高清久久| 久久婷婷丁香 | 在线观看国产欧美 | 亚洲免费精品视频 | 中文字幕一区在线 | 一区二区三区国产视频 | 蜜桃av免费在线观看 | 黄在线观看 | 久久99国产精品 | 三级国产在线观看 | 欧美精品系列 | 91视频最新网址 | 精品不卡一区 | 欧美一区一区 | 天堂在线免费观看视频 | aaa国产| 久久午夜伦理 | 欧洲中文字幕 | 色99999| 成人久久久久久久 | 四虎国产成人永久精品免费 | 伊人久久久久久久久久 | 香蕉av在线播放 | 欧美日韩中 | 老司机黄色影院 | 欧美综合一区二区 | 人人草在线视频 | 国产成人精品久久 | 免费看黄在线观看 | 24小时日本在线www免费的 | 黄色男女视频 | 欧美激情在线播放 | 成人在线观看网址 | 日本天堂在线播放 | 亚洲日本va | 国产高清在线视频 |