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
wly
V2EX  ›  Python

python web.py 下的全局变量的问题

  •  1
     
  •   wly · 2016-08-05 10:06:45 +08:00 · 3787 次点击
    这是一个创建于 2792 天前的主题,其中的信息可能已经有所发展或是发生改变。
    
    import threading
    import time
    import web
    
    global bianliang
    bianliang=0
    
    class Test:
        global bianliang
        def GET(self):
            return str(bianliang)
    
    def _listen_yushui():
        while True:
            time.sleep(2)
            global bianliang
            print bianliang
    
    def _add():
        while True:
            time.sleep(2)
            global bianliang
            bianliang=bianliang+1
    urls=(
        '/test','Test'
    )
    application = web.application(urls, globals())
    
    if __name__ == "__main__":
        yushui = threading.Thread(target=_listen_yushui)
        yushui.setDaemon(True)
        yushui.start()
        add = threading.Thread(target=_add)
        add.setDaemon(True)
        add.start()
        application.run()
    

    上面的代码在运行时,控制台输出可以看到 bianliang 的值在不断的增加;但是发送一个网络请求'your_url/test',请求到的结果却是 bianliang 的初始值 0 ,这是为什么?

    5 条回复    2022-11-15 17:07:50 +08:00
    iii
        1
    iii  
       2016-08-05 10:35:02 +08:00
    控制台输出和 webserver 属于不同进程
    vmebeh
        2
    vmebeh  
       2016-08-05 10:48:41 +08:00 via iPhone
    用过 web.var_name="value"
    wly
        3
    wly  
    OP
       2016-08-05 11:56:28 +08:00
    @vmebeh 感谢,已成功
    zhuangzhuang1988
        4
    zhuangzhuang1988  
       2016-08-05 12:38:34 +08:00
    redis 上.
    HankLu
        5
    HankLu  
       2022-11-15 17:07:50 +08:00
    @wly 请问怎么改的?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3232 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 11:57 · PVG 19:57 · LAX 04:57 · JFK 07:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.