2

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?

Community
  • 1
  • 1
Juan Carlos Coto
  • 11,900
  • 22
  • 62
  • 102
  • Why do you need this? Understanding the use case beyond a reference to separate abstract situation may be helpful and may lead to other ideas that don't need this type of solution. Checking out the source (http://code.google.com/p/googleappengine/source/browse/trunk/python/dev_appserver.py) may help as well. – bossylobster Oct 08 '12 at 20:27
  • @bossylobster We need this to have a set-up which uses the remote_api for local database access, to help us debug some use cases where it would be otherwise too expensive or difficult to do. We already have tested remote api functions, so we **can** retrieve data from the remote datastore, but when writing data to the response of the WSGI application, we get an error which we believe has to do with datastore conflict. – Juan Carlos Coto Oct 09 '12 at 15:06
  • If you want to inspect the local datastore, navigate to `http://localhost:PORT/_ah/admin/interactive` where `PORT` is the port you are using for your local app server. You can execute arbitrary code there against the application. – bossylobster Oct 10 '12 at 01:43

1 Answers1

3

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.

Guido van Rossum
  • 16,690
  • 3
  • 46
  • 49
  • Thanks, Guido. Where is appengine_config.py located? – Juan Carlos Coto Oct 09 '12 at 15:03
  • In the root of your application. – bossylobster Oct 10 '12 at 01:42
  • When I add the remote api initialization code to appengine_config.py, I get this error: `File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_blobstore.py", line 79, in GetBlobStorage return apiproxy_stub_map.apiproxy.GetStub('blobstore').storage AttributeError: 'RemoteStub' object has no attribute 'storage'` – Juan Carlos Coto Oct 10 '12 at 17:33
  • Possibly http://stackoverflow.com/questions/12413826/remote-api-configuration-with-app-engine can help with that message? – Guido van Rossum Oct 11 '12 at 16:14