Web 应用启动多次 - web.py

Web app starts many times - 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 但它解决了烧瓶中的问题,我使用 web.py

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