python - Web 应用程序启动多次 - web.py

标签 python python-2.7 web.py

我有这段代码,它在服务器启动时加载必要的文件并打印必要的信息,但在 if __name__ == "__main__": 我也启动了一个后台进程,然后最后是 app. run() 被执行。

我的问题是在加载所有内容并开始后台进程后,它开始重新打印并加载所有内容。当服务器收到第一个请求(GET/POST)时,它也会执行相同的操作。我怎样才能让它只加载一次?

import web
from multiprocessing import Process
import scripts
print 'Engine Started'
# Code to load and print necessary stuff goes here...

urls = (
    '/test(.*)', 'Test'
)

class Test():

    def GET(self,r):
        tt = web.input().t
        print tt
        return tt


if __name__ == "__main__":
    try:
        print 'Cache initializing...'
        p = Process(target=scripts.initiate_cleaner)
        p.start() # Starts the background process
    except Exception, err:
        print "Error initializing cache"
        print err

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

因此,在启动进程并从 localhost:8080/test?t=er 发出请求后,会加载三次(“Engine Started”打印三次)

我浏览了this但它解决了 Flask 中的问题,我使用 web.py

最佳答案

我不确定为什么这会让您感到惊讶,或者为什么这会成为一个问题。根据定义,后台进程是独立于 Web 进程的进程;其中每个都将导入代码,因此打印该消息。如果您不想看到该消息,请将其放入 if __name__ block 内。

关于python - Web 应用程序启动多次 - web.py,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37133738/

相关文章:

python - Python 中的 4D 密度图

python - 从 C 调用 python 回调时如何修复 "SystemError: null argument to internal routine"错误

python - 如何在 python 2.7 中循环遍历 if 语句中的选项列表?

sql - Python 中的动态 INSERT 语句

python - ncpol2sdpa python工具中出现错误 'OSError: [Errno 2] No such file or directory'的原因是什么?

python - 如何在 __main__ 中创建一个变量,供同一文件中定义的 web.py 类使用?

python - 打开简历,img [x,y]总是返回0

python - 如何使用Python子进程添加到VLC播放列表队列

python - Webpy session : AttributeError: 'ThreadedDict' object has no attribute 'username'

python - Github api v3 通过 python oauth2 库访问 - 重定向问题