由于GIL的原因,笔者在日常开发中几乎没有用到python的多线程。如果需要并发,一般使用多进程,对于IO Bound这种情况,使用协程也是不错的注意。但是在python很多的网络库中,都支持多线程,基本上都会使用到threading.local。在python中threading.local用来表示线程相关的数据,线程相关指的是这个属性再各个线程中是独立的 互不影响,先来看一个最简答的例子:
   

 class Widgt(object):
pass import threading
def test():
local_data = threading.local()
# local_data = Widgt()
local_data.x = 1 def thread_func():
print('Has x in new thread: %s' % hasattr(local_data, 'x'))
local_data.x = 2 t = threading.Thread(target = thread_func)
t.start()
t.join()
print('x in pre thread is %s' % local_data.x) if __name__ == '__main__':
test()
输出:
    Has x in new thread: False
    x in pre thread is 1
 
  可以看到,在新的线程中 local_data 并没有x属性,并且在新线程中的赋值并不会影响到其他线程。也可以稍微改改代码,去掉第7行的注释,local_data就变成了线程共享的变量。
 
  local怎么实现的呢 在threading.py 代码如下:
  

 try:
from thread import _local as local
except ImportError:
from _threading_local import local
  可以看到,local是python的buildin class,同时也提供了一个纯python版本的参考实现,在_threading_local.py,我们来看看代码(代码不全 省略了几个函数):
  

 class _localbase(object):
__slots__ = '_local__key', '_local__args', '_local__lock' def __new__(cls, *args, **kw):
self = object.__new__(cls)
key = '_local__key', 'thread.local.' + str(id(self)) # 产生一个key,这个key在同一个进程的多个线程中是一样的
object.__setattr__(self, '_local__key', key)
object.__setattr__(self, '_local__args', (args, kw))
object.__setattr__(self, '_local__lock', RLock()) # 可重入的锁 if (args or kw) and (cls.__init__ is object.__init__):
raise TypeError("Initialization arguments are not supported") # We need to create the thread dict in anticipation of
# __init__ being called, to make sure we don't call it
# again ourselves.
dict = object.__getattribute__(self, '__dict__')
current_thread().__dict__[key] = dict # 在current_thread这个线程唯一的对象的—__dict__中加入 key return self def _patch(self):
key = object.__getattribute__(self, '_local__key')
d = current_thread().__dict__.get(key) # 注意 current_thread 在每一个线程是不同的对象
if d is None: # 在新的线程第一次调用时
d = {} # 一个空的dict !!!
current_thread().__dict__[key] = d
object.__setattr__(self, '__dict__', d) # 将实例的__dict__赋值为 线程独立的一个字典 # we have a new instance dict, so call out __init__ if we have
# one
cls = type(self)
if cls.__init__ is not object.__init__:
args, kw = object.__getattribute__(self, '_local__args')
cls.__init__(self, *args, **kw)
else:
object.__setattr__(self, '__dict__', d) class local(_localbase): def __getattribute__(self, name):
lock = object.__getattribute__(self, '_local__lock')
lock.acquire()
try:
_patch(self) # 这条语句执行之后,self.__dict__ 被修改成了线程独立的一个dict
return object.__getattribute__(self, name)
finally:
lock.release()
   代码中 已经加入了注释,便于理解。总结就是,在每个线程中增加一个独立的dict(通过current_thread()这个线程独立的对象),然后每次对local实例增删改查的时候,进行__dict__的替换。我们看看测试代码:
  

 import threading
from _threading_local import local
def test():
local_data = local()
local_data.x = 1
print 'id of local_data', id(local_data) def thread_func():
before_keys = threading.current_thread().__dict__.keys()
local_data.x = 2
after = threading.current_thread().__dict__
# print set(after.keys()) - set(before.keys())
print [(e, v) for (e, v) in after.iteritems() if e not in before_keys] t = threading.Thread(target = thread_func)
t.start()
t.join()
print('x in pre thread is %s' % local_data.x) if __name__ == '__main__':
test()

输出:

  id of local_data 40801456
  [(('_local__key', 'thread.local.40801456'), {'x': 2})]

  从输出可以看到,在这次运行总,local_data的id是40801456,在每个线程中都是一样的。在新的线程(thread_func函数)中访问local_data对象之前,current_thread()返回的对象是没有__local_key的,在第10行访问的时候会增加这个属性(_patch函数中)。
  
  在gevent中,也有一个类叫local,其作用是提供协程独立的数据。PS:gevent中提供了几乎与python原生协程一样的数据结构,如Event、Semaphore、Local,而且,gevent的代码和文档中也自称为“thread”,这点需要注意。gevent.local的实现借鉴了上面介绍的_threading_local.py, 区别在于,_threading_local.local 将线程独立的数据存放在current_thread()中,而gevent.local将协程独立的数据存放在greenlet.getcurrent()中。
 
   最后,如果在代码中使用了gevent.monkey.patch_all(),那么python原生的threading.local将会被替换成gevent.local.local。之前在看bottle的代码的时候,发现里面都是使用的threading.local,当时也对monkey_patch具体patch了那些模块不了解,于是就想如果使用gevent是否会出错呢,结果测试了很久都发现没问题,直到重新细看bottle源码才发现原因所在。代码如下:
  

 class GeventServer(ServerAdapter):
""" Untested. Options: * See gevent.wsgi.WSGIServer() documentation for more options.
""" def run(self, handler):
from gevent import pywsgi, local
if not isinstance(threading.local(), local.local): #注意这里
msg = "Bottle requires gevent.monkey.patch_all() (before import)"
raise RuntimeError(msg)
if self.quiet:
self.options['log'] = None
address = (self.host, self.port)
server = pywsgi.WSGIServer(address, handler, **self.options)
if 'BOTTLE_CHILD' in os.environ:
import signal
signal.signal(signal.SIGINT, lambda s, f: server.stop())
server.serve_forever()

  这个小插曲其实也反映了monkey-patch的一些优势与劣势。其优势在于不对源码修改就能改变运行时行为,提高性能;同时 ,对于缺乏经验或者对patch细节不了解的人来说,会带来静态代码与运行结果之间的认知差异。

 
references:
 
 
 
 
 
 
 
 
 
 

Python thread local的更多相关文章

  1. TLS 与 python thread local

    TLS 先说TLS( Thread Local Storage),wiki上是这么解释的: Thread-local storage (TLS) is a computer programming m ...

  2. Python - python3.7新增的contextvars vs Thread local(threading.local)

    总结 和threading.local()类似.Python3.7新增. thread.local(): 不同线程,同一个变量保存不同的值. contextvars: 不同上下文,同一个变量保存不同的 ...

  3. [Python]threading local 线程局部变量小測试

    概念 有个概念叫做线程局部变量.一般我们对多线程中的全局变量都会加锁处理,这样的变量是共享变量,每一个线程都能够读写变量,为了保持同步我们会做枷锁处理.可是有些变量初始化以后.我们仅仅想让他们在每一个 ...

  4. Java进阶(七)正确理解Thread Local的原理与适用场景

    原创文章,始自发作者个人博客,转载请务必将下面这段话置于文章开头处(保留超链接). 本文转发自技术世界,原文链接 http://www.jasongj.com/java/threadlocal/ Th ...

  5. 线程TLAB局部缓存区域(Thread Local Allocation Buffer)

    TLAB(Thread Local Allocation Buffer) 1,堆是JVM中所有线程共享的,因此在其上进行对象内存的分配均需要进行加锁,这也导致了new对象的开销是比较大的 2,Sun ...

  6. Java Thread Local – How to use and code sample(转)

    转载自:https://veerasundar.com/blog/2010/11/java-thread-local-how-to-use-and-code-sample/ Thread Local ...

  7. Python Thread related

    1.Thread.join([timeout]) Wait until the thread terminates. This blocks the calling thread until the ...

  8. python thread的join方法解释

    python的Thread类中提供了join()方法,使得一个线程可以等待另一个线程执行结束后再继续运行.这个方法还可以设定一个timeout参数,避免无休止的等待.因为两个线程顺序完成,看起来象一个 ...

  9. 【Python@Thread】queue模块-生产者消费者问题

    python通过queue模块来提供线程间的通信机制,从而可以让线程分项数据. 个人感觉queue就是管程的概念 一个生产者消费者问题 from random import randint from ...

随机推荐

  1. linux命令学习-4-lsof

    lsof(list open files)是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件. 在终端下输入ls ...

  2. iOS 架构模式

    参考:http://www.cocoachina.com/ios/20160108/14916.html MVC , MVP , MVVM , VIPER

  3. 在ubtuntu中使用包管理器 linux-brew

    用惯了mac的程序员回到linux开发平台总觉得有点不适应,这是因为linux的界面没有mac那么精美,而且linux的包管理器没有mac上面的homebrew那么强大.mac程序员遇到库的依赖问题时 ...

  4. 在新浪sae上部署WeRoBot

    花了整整一个下午,终于在新浪sae部署完成WeRoBot,现在将其中的曲折记录下来. 首先下载WeRoBot-SAE-demo,按照README.md中的要求,执行下述命令: git clone gi ...

  5. PHP导入导出Excel方法

    看到这篇文章的时候,很是惊讶原作者的耐心,虽然我们在平时用的也 有一些,但没有作者列出来的全,写excel的时候,我用过pear的库,也用过pack压包的头,同样那些利用smarty等作的简单替换xm ...

  6. js模块化开发——require.js的实战写法1

    关于在Require.js使用一个JS插件的问题 我需要在项目中引用一个js控件,这个控件依赖于a.js,b.js,c.js,.....n.js N多个js以及jquery及jquery-ui,各js ...

  7. 在iOS微信浏览器中自动播放HTML5 audio(音乐)的2种正确方式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Java打印

    Java打印 import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Gra ...

  9. eclipse jsp html 格式化 format

    eclipse jsp html 格式化 format http://blog.csdn.net/hexin373/article/details/7677250

  10. web前端性能调优(二)

    项目经过第一波优化之后APP端已基本已经符合我们的要求了,但是TV端还是反应比较慢,页面加载和渲染都比较慢了一点,我觉的还是有必要在进行一些优化,经过前面的优化,我们的优化空间已经小了一部分,不过还是 ...