web.py 运行 main 两次,忽略更改 [英] web.py running main twice, ignoring changes

查看:50
本文介绍了web.py 运行 main 两次,忽略更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 web.py 应用程序,它读取一个配置文件并提供给 URL 路径.但是我有两个奇怪的行为.一,对 Main 中数据所做的更改不会反映在 GET 的结果中.二,Main 似乎运行了两次.

期望的行为是修改 Main 中的数据会导致方法看到修改后的数据,而不是重新运行 main.

问题:

  1. 这里到底发生了什么, mydict 都没有被修改得到.
  2. 为什么我的某些代码运行了两次.
  3. 实现所需行为的最简单途径(最重要)
  4. 实现所需行为的 Pythonic 路径(最不重要)

来自 pbuck(接受的答案):3.) 的答案是替换

app = web.application(urls, globals())

与:

app = web.application(urls, globals(), autoreload=False)

pythons Linux (CentOS 6 python 2.6.6) 和 MacBook (brew python 2.7.12) 上的相同行为

开始时我得到:

$ python ./foo.py 8080初始化 mydict修改 mydicthttp://0.0.0.0:8080/

查询时:

wget http://localhost:8080/node/first/foowget http://localhost:8080/node/second/bar

结果(注意第二个初始化 mydict"):

初始化 mydictfirstClass.GET 用 clobber foo 调用firstClass.GET somevalue 是静态的127.0.0.1:52480 - - [17/Feb/2017 17:30:42]HTTP/1.1 GET/node/first/foo" - 200 OKsecondClass.GET 用 clobber bar 调用secondClass.GET somevalue 是静态的127.0.0.1:52486 - - [17/Feb/2017 17:30:47]HTTP/1.1 GET/node/second/bar" - 200 OK

代码:

#!/usr/bin/python导入网页网址 = ('/node/first/(.*)', 'firstClass','/node/second/(.*)', 'secondClass')# 初始化web服务器,稍后在app .run()"处启动#app = web.application(urls, globals())# 在 Main 或更高版本中运行 web.application 不会改变行为# 静态初始化 mydict打印初始化 mydict"我的字典 = {}mydict['somevalue'] = "静态的东西"第一类:def GET(自我,globarg):打印用 clobber %s 调用的 firstClass.GET" % globarg打印firstClass.GET somevalue 是 %s" % mydict['somevalue']返回 mydict['somevalue']第二类:def GET(自我,globarg):打印用 clobber %s 调用的 secondClass.GET" % globarg打印 "secondClass.GET somevalue is %s" % mydict['somevalue']返回 mydict['somevalue']如果 __name__ == '__main__':app = web.application(urls, globals())# 在这里读取初始化配置文件打印修改 mydict"mydict['somevalue'] = "东西动态"应用程序运行()

解决方案

简而言之,避免使用全局变量,因为它们不会做您认为它们会做的事情.特别是当您最终在 nginx/apache 下部署它时,其中(可能)会运行多个进程.

更长的答案

<块引用>

  1. 为什么我的某些代码会运行两次?

app.py 的全局代码运行两次,因为它像往常一样运行一次.第二次是在 web.application(urls, globals()) 调用中.真的,对 globals() 的调用设置了模块加载/重新加载.其中一部分是重新加载所有模块(包括 app.py).如果您在 web.applications() 调用中设置 autoreload=False,则不会这样做.

<块引用>

  1. 这里到底发生了什么,在任何一个 GET 中都没有修改 mydict?

mydict 被设置为动态的东西",但在第二次加载时被重新设置为静态的东西".再次设置 autoreload=False,它会按您的预期工作.

<块引用>

  1. 最短路径?

autoreload=False

<块引用>

  1. Pythonic 路径?

.... 好吧,我想知道为什么你有 mydict['somevalue'] = 'something static' and mydict['somevalue'] ='something dynamic' 在你的模块中以这种方式:为什么不只在 '__main__' 下设置一次?

I have a simple web.py app that reads a config file and serves to URL paths. However I get two strange behaviors. One, changes made to data in the Main are not reflected in the results of GET. Two, Main appears to run twice.

Desired behavior is modifying data in Main will cause methods to see modified data, and not having main re-run.

Questions:

  1. What is really happening here, that mydict is not modified in either GET.
  2. Why am I getting some code running twice.
  3. Simplest path to desired behavior (most important)
  4. Pythonic path to desired behavior (least important)

From pbuck (Accepted Answer): Answer for 3.) is replace

app = web.application(urls, globals())

with:

app = web.application(urls, globals(), autoreload=False)

Same behavior on pythons Linux (CentOS 6 python 2.6.6) and MacBook (brew python 2.7.12)

When started I get:

$ python ./foo.py 8080
Initializing mydict
Modifying mydict
http://0.0.0.0:8080/

When queried with:

wget http://localhost:8080/node/first/foo
wget http://localhost:8080/node/second/bar

Which results in (notice a second "Initializing mydict"):

Initializing mydict
firstClass.GET called with clobber foo
firstClass.GET somevalue is something static
127.0.0.1:52480 - - [17/Feb/2017 17:30:42] "HTTP/1.1 GET /node/first/foo" - 200 OK
secondClass.GET called with clobber bar
secondClass.GET somevalue is something static
127.0.0.1:52486 - - [17/Feb/2017 17:30:47] "HTTP/1.1 GET /node/second/bar" - 200 OK

Code:

#!/usr/bin/python
import web

urls = (
    '/node/first/(.*)', 'firstClass',
    '/node/second/(.*)', 'secondClass'
    )

# Initialize web server, start it later at "app . run ()"
#app = web.application(urls, globals())
# Running web.application in Main or above does not change behavior

# Static Initialize mydict
print "Initializing mydict"
mydict = {}
mydict['somevalue'] = "something static"

class firstClass:
    def GET(self, globarg):
        print "firstClass.GET called with clobber %s" % globarg
        print "firstClass.GET somevalue is %s" % mydict['somevalue']
        return mydict['somevalue']

class secondClass:
    def GET(self, globarg):
        print "secondClass.GET called with clobber %s" % globarg
        print "secondClass.GET somevalue is %s" % mydict['somevalue']
        return mydict['somevalue']

if __name__ == '__main__':
    app = web.application(urls, globals())
    # read configuration files for initializations here
    print "Modifying mydict"
    mydict['somevalue'] = "something dynamic"
    app.run()

解决方案

Short answer, avoid using globals as they don't do what you think they do. Especially when you eventually deploy this under nginx / apache where there will (likely) be multiple processes running.

Longer answer

  1. Why am I getting some code running twice?

Code, global to app.py, is running twice because it runs once, as it normally does. The second time is within the web.application(urls, globals()) call. Really, that call to globals() sets up module loading / re-loading. Part of that is re-loading all modules (including app.py). If you set autoreload=False in the web.applications() call, it won't do that.

  1. What is really happening here, that mydict is not modified in either GET?

mydict is getting set to 'something dynamic', but then being re-set to 'something static' on second load. Again, set autoreload=False and it will work as you expect.

  1. Shortest path?

autoreload=False

  1. Pythonic path?

.... well, I wonder why you have mydict['somevalue'] = 'something static' and mydict['somevalue'] = 'something dynamic' in your module this way: why not just set it once under '__main__'?

这篇关于web.py 运行 main 两次,忽略更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆