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 ,这是为什么?
1
iii 2016-08-05 10:35:02 +08:00
控制台输出和 webserver 属于不同进程
|
2
vmebeh 2016-08-05 10:48:41 +08:00 via iPhone
用过 web.var_name="value"
|
4
zhuangzhuang1988 2016-08-05 12:38:34 +08:00
redis 上.
|