【发布时间】:2016-09-07 00:24:00
【问题描述】:
我需要在我的程序中使用全局变量,烧瓶或瓶子作为 Web 服务运行。到目前为止,我在这里找到了带有 sn-p 的瓶子作为线程:Starting python bottle in a thread/Process and another daemon next to it
我基本上是想去localhost:8080/hello 把全局变量test加一:
#!/usr/bin/env python
from bottle import route, run
from multiprocessing import Process
@route('/hello')
def hello():
global test
test = test + 1
return test
def main():
global test
test = 0
t = Process(target=bottle.run(host='0.0.0.0', port=8080))
t.daemon = True
t.start()
while(True)
print test
time.sleep(0.5)
if __name__ == "__main__":
main()
如果我用浏览器访问 localhost:8080/hello,我会得到: 错误 500:内部服务器错误 - 未处理的异常
我看不到异常,即使有
try
global test
test = test + 1
return test
except Exception e
print e
【问题讨论】:
-
你是如何开始你的程序的?您应该会在 stderr 上看到错误(回溯)。
-
您发布的代码不会运行,它有语法错误。您确定发布的代码与您实际运行的代码相同吗?例如。
while(True)行是语法错误。