0
votes

I'm building an application using Flask running locally on the Google App Engine SDK. For some reason my entrypoint, being main.py, is only initiated once.

main.py:

from bootstrap import myapp

app = myapp.bootstrap()

print "I'm bootstrapping"

if __name__ == '__main__':
    app.run()

myapp/bootstrap.py:

Returns an instance of MyApp.Application

myapp/Application.py:

Extends Flask, registers the routes,...

Console output:

INFO 2016-10-24 20:02:10,807 module.py:788] default: "GET / HTTP/1.1" 200 12
INFO 2016-10-24 20:02:11,595 module.py:788] default: "GET / HTTP/1.1" 200 12
INFO 2016-10-24 20:02:20,137 module.py:402] [default] Detected file changes:
./main.py
I'm bootstrapping
INFO 2016-10-24 20:02:22,199 module.py:788] default: "GET / HTTP/1.1" 200 12
INFO 2016-10-24 20:02:28,206 module.py:788] default: "GET / HTTP/1.1" 200 12

The request is handled correctly and the route functions are called every time. main.py on the other hand is only loaded once and only reloaded when a file has changed (as you can see from the logs). It seems like the whole application is initiated once and cached.

Also: Values you assign within a route-method to the application instance are also 'cached' and available in further requests (which seems like weird behaviour).

Is this behaviour normal in Python? Or is this behaviour specific to Google App Engine?

1
This is normal behaviour. The app instance keeps running and the appengine infrastructure will send requests to available instances. I suggest you have a read up on how the sandbox works.Tim Hoffman
@TimHoffman: I was typing my answer as you posted your comment. Your comment indeed describes my problem. Thanks.Lowie Huyghe

1 Answers

0
votes

Thanks to a colleague I have found the problem.
I have a PHP background and am currently improving my Python. In PHP with each and every request the application is completely build from ground up. In Python on the other hand, your application is initiated once and will keep running. Requests will be handled by the same running instance.

So in my case the application was initiated once, printing "I'm bootstrapping". On file-change the application was reinitiated because of the changes. Further request will be processed by the same running application with the same properties.