码迷,mamicode.com
首页 > 其他好文 > 详细

thread local变量

时间:2018-04-10 04:30:40      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:thread local变量

import threading try:     from greenlet import getcurrent as get_ident # 协程 except ImportError:     try:         from thread import get_ident     except ImportError:         from _thread import get_ident # 线程 class Local(object):     def __init__(self):         object.__setattr__(self, '__storage__', {})         object.__setattr__(self, '__ident_func__', get_ident)     def __getattr__(self, name):         try:             return self.__storage__[self.__ident_func__()][name]         except KeyError:             raise AttributeError(name)     def __setattr__(self, name, value):         ident = self.__ident_func__()         storage = self.__storage__         try:             storage[ident][name] = value         except KeyError:             storage[ident] = {name: value}     def __delattr__(self, name):         try:             del self.__storage__[self.__ident_func__()][name]         except KeyError:             raise AttributeError(name) local_values = Local() def task(num):     local_values.name = num     import time     time.sleep(1)     print(local_values.name, threading.current_thread().name) for i in range(20):     th = threading.Thread(target=task, args=(i,),name='线程%s' % i)     th.start()


thread local变量

标签:thread local变量

原文地址:http://blog.51cto.com/12765315/2096304

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!