mysql jdbc銜接步調及罕見參數。本站提示廣大學習愛好者:(mysql jdbc銜接步調及罕見參數)文章只能為提供參考,不一定能成為您想要的結果。以下是mysql jdbc銜接步調及罕見參數正文
比來被多線程給坑了下,沒認識到類變量在多線程下是同享的,還有一個就是沒認識到 內存釋放成績,招致越累越年夜
1.python 類變量 在多線程情形 下的 是同享的
2.python 類變量 在多線程情形 下的 釋放是不完整的
3.python 類變量 在多線程情形 下沒釋放的那部門 內存 是可以反復應用的
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)
總結
公用的數據,除非是只讀的,否則不要當類成員變量,一是會同享,二是欠好釋放。