问题
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.
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()
So this loads thrice ('Engine Started' prints thrice) after starting process and requesting from localhost:8080/test?t=er
I went through this but it solves the problem in flask and I use web.py
回答1:
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.
来源:https://stackoverflow.com/questions/37133738/web-app-starts-many-times-web-py