欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Python下線程之間的共享和釋放示例

系統(tǒng) 1610 0

最近被多線程給坑了下,沒意識到類變量在多線程下是共享的,還有一個就是沒意識到 內(nèi)存釋放問題,導(dǎo)致越累越大

1.python 類變量 在多線程情況 下的 是共享的

2.python 類變量 在多線程情況 下的 釋放是不完全的

3.python 類變量 在多線程情況 下沒釋放的那部分 內(nèi)存 是可以重復(fù)利用的

            
import threading
 import time
  
 class Test:
  
   cache = {}
    
   @classmethod
   def get_value(self, key):
     value = Test.cache.get(key, [])
     return len(value)
  
   @classmethod
   def store_value(self, key, value):
     if not Test.cache.has_key(key):
       Test.cache[key] = range(value)
     else:
       Test.cache[key].extend(range(value))
     return len(Test.cache[key])
  
   @classmethod
   def release_value(self, key):
     if Test.cache.has_key(key):
       Test.cache.pop(key)
     return True
  
   @classmethod
   def print_cache(self):
     print 'print_cache:'
     for key in Test.cache:
       print 'key: %d, value:%d' % (key, len(Test.cache[key]))
  
 def worker(number, value):
   key = number % 5
   print 'threading: %d, store_value: %d' % (number, Test.store_value(key, value))
   time.sleep(10)
   print 'threading: %d, release_value: %s' % (number, Test.release_value(key))
  
 if __name__ == '__main__':
   thread_num = 10
    
   thread_pool = []
   for i in range(thread_num):
     th = threading.Thread(target=worker,args=[i, 1000000])
     thread_pool.append(th)
     thread_pool[i].start()
  
   for thread in thread_pool:
     threading.Thread.join(thread)
    
   Test.print_cache()
   time.sleep(10)
    
   thread_pool = []
   for i in range(thread_num):
     th = threading.Thread(target=worker,args=[i, 100000])
     thread_pool.append(th)
     thread_pool[i].start()
  
   for thread in thread_pool:
     threading.Thread.join(thread)
    
   Test.print_cache()
   time.sleep(10)

          

總結(jié)

公用的數(shù)據(jù),除非是只讀的,不然不要當(dāng)類成員變量,一是會共享,二是不好釋放。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: www.亚洲黄色 | 一个人看aaaa免费中文 | 久草手机视频在线观看 | 中文字幕日韩欧美 | 中文字幕 在线观看 | 国产成人一区二区精品非洲 | 一区二区三区四区精品 | 大伊香蕉精品视频在线天堂 | 中文字幕二区 | 中文字幕一区二区三区四区 | 初女破苞国语在线观看免费 | 凤囚凰 电视剧 | 成人爽a毛片免费啪啪红桃视频 | 日本 亚洲 欧美 | 婷婷色中文字幕 | 久久网在线| 色综合久久98天天综合 | 久操视频在线观看 | 精品中文字幕久久久久久 | 欧洲成人免费视频 | 狠狠躁夜夜躁人人爽视频 | 老司机福利在线视频 | 成人网页 | 国内精品久久毛片一区二区 | 9999毛片免费看 | 成人天堂网 | 亚洲精品欧美一区二区三区 | 国产日韩欧美中文字幕 | 91婷婷韩国欧美一区二区 | 国产一级一级一级成人毛片 | 亚洲国产精品一区二区久久 | 国产1级| 激情九月 | 国产精品爱啪在线线免费观看 | 超碰综合 | 天天干天天操天天爽 | 久久美女 | 天堂中文网| 国产国产成人久久精品杨幂 | 污污视频免费网站 | 欧美在线一区二区三区欧美 |