【问题标题】:Globale Variables with Bottlpy (in background)带瓶的全局变量(在后台)
【发布时间】: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) 行是语法错误。

标签: python bottle


【解决方案1】:

您从 hello 返回一个 int,但您需要返回一个可迭代的(字符串)。

提示:将debug=True 添加到您对run 的调用中,以便在您的回复中获得更好的错误信息。

t = Process(target=run(host='0.0.0.0', port=8080, debug=True))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    相关资源
    最近更新 更多