Web 应用程序多次启动 - web.py [英] Web app starts many times - web.py

查看:35
本文介绍了Web 应用程序多次启动 - web.py的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this code where it loads necessary files and prints necessary information when the server starts but inside if __name__ == "__main__": I'm starting a background process as well then finally app.run() is executed.

我的问题是在加载完所有内容并开始后台进程后,它开始打印并从头开始加载所有内容.当服务器收到第一个请求(GET/POST)时,它也会做同样的事情.我怎样才能让它只加载一次?

My problem is after loading all and comes to the starting of background process it starts to print and load everything from beginning again. And also it does the same when the server get its first request (GET/POST). How can I make it load only once?

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' 打印三次)

So this loads thrice ('Engine Started' prints thrice) after starting process and requesting from localhost:8080/test?t=er

我经历了这个但是它解决了烧瓶中的问题,我使用 web.py

I went through this but it solves the problem in flask and I use web.py

推荐答案

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

I'm not sure why this surprises you, or why it would be a problem. A background process is by definition a separate process from the web process; each of those will import the code, and therefore print that message. If you don't want to see that message, put it inside the if __name__ block.

这篇关于Web 应用程序多次启动 - web.py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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