devappserver2, remote_api, and --default_partition

2019-04-10 03:00发布

问题:

To access a remote datastore locally using the original dev_appserver I would set --default_partition=s as mentioned here

In March 2013 Google made devappserver2 the default development server, and it does not support --default_partition resulting in the original, dreaded:

BadRequestError: app s~appname cannot access app dev~appname's data

It appears like the first few requests are served correctly with

os.environ["APPLICATION_ID"] == 's~appname'

Then a subsequent request results in a call to /_ah/warmup and then

os.environ["APPLICATION_ID"] == 'dev~appname'

The docs specifically mention related topics but appear geared to dev_appserver here

Warning! Do not get the App ID from the environment variable. The development server simulates the production App Engine service. One way in which it does this is to prepend a string (dev~) to the APPLICATION_ID environment variable, which is similar to the string prepended in production for applications using the High Replication Datastore. You can modify this behavior with the --default_partition flag, choosing a value of "" to match the master-slave option in production. Google recommends always getting the application ID using the get_application_id() method, and never using the APPLICATION_ID environment variable.

回答1:

You can do the following dirty little trick:

from google.appengine.datastore.entity_pb import Reference

DEV = os.environ['SERVER_SOFTWARE'].startswith('Development')

def myApp(*args): 
    return os.environ['APPLICATION_ID'].replace("dev~", "s~")

if DEV:
    Reference.app = myApp