V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
polythene
V2EX  ›  Python

[求助]关于threading模块中local类的用法

  •  
  •   polythene ·
    polyrabbit · 2013-02-24 18:12:14 +08:00 · 3104 次点击
    这是一个创建于 4095 天前的主题,其中的信息可能已经有所发展或是发生改变。
    https://gist.github.com/polyrabbit/5023268
    一个threading.local的实例相当于获得了当前线程的上下文环境,我先设置一个值再读取它为什么就读不到了呢??

    如果改成这样就可以读到:
    https://gist.github.com/polyrabbit/5023291
    难道在同一线程是两次调用threading.local返回的环境是不一样的?求解。。

    P.S. 其实我就是想将一个状态量在两个模块间传递,但又不想用传参(参数太多了)和加锁(太麻烦)的方法,不知道用thread.local该怎么做

    PP.S 怎么嵌入gist代码呀??
    1 条回复    1970-01-01 08:00:00 +08:00
    lookhi
        1
    lookhi  
       2013-02-24 20:14:08 +08:00
    看_threading_local 这个module,python版本的local.
    每次local()都是不一样的,因此local()必须放在global的层次上。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5219 人在线   最高记录 6547   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 09:48 · PVG 17:48 · LAX 02:48 · JFK 05:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.