Sharing and releasing python variables in multiple threads

Source: Internet
Author: User
The sharing and release of python class variables under multiple threads has recently been put into the trap by multiple threads. I am not aware that class variables are shared under multiple threads. Another problem is that I am not aware of the memory release problem, the more tired

1. python variables are shared in the case of multiple threads.

2. release of python variables in multi-thread mode is incomplete.

3. the memory that is not released when python variables are multithreading can be reused.

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)

Public data, unless read-only, should not be a class member variable. one is shared, and the other is not easy to release.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.