When running a Python Web Application on App Engine, we need to set up some mechanism to execute some code before (or during) the application’s initialization. This means that, in the optimal solution, the code that we need to run is executed as early as possible. The purpose of this is to allow for the App Engine remote_api to be initialized before the local datastore is accessed, so as to prevent datastore access conflicts.
This is a very rough example of what we’re looking for:
imports (including remote_api)
def some_initialization_function_or_similar (args):
some_init_function_calls(...)
setup_remote_api(...)
access_datastore_the_first_time(...)
Please take this question as reference to the scenario we’re looking at: Using GAE remote api for debugging from localhost – Connecting too late?
Assuming you have a single entry point (== script named in app.yaml) you can just call the desired code after your imports but before you call your main() function. This means it will be run only when the main script is being imported, which is only on the first request (hitting that entry point).
If you have multiple entry points, try putting it in appengine_config.py. That gets loaded pretty early.