How to get a long instance_id form for an App Engine instance?

I can get a short integer instance_id instance for the running process by calling modules.get_current_instance_id() , but this returns the integer index to the list of instances for the current version of the application. I can do rpc to get the full hex instance_id as follows:

 from google.appengine.api.app_identity import app_identity from googleapiclient.discovery import build from oauth2client.client import GoogleCredentials credentials = GoogleCredentials.get_application_default() service = build('appengine', 'v1', credentials=credentials) appsId = app_identity.get_application_id() rpc_result = service.apps().services().versions().instances().list(versionsId='23', servicesId='default', appsId=appsId).execute() print rpc_result['instances'][int(modules.get_current_instance_id())] 

This gives me a dict that contains, among other things, a key-value pair 'id' , where the value looks something like this: 00c61b117cb3c50973d6a73225b3d807eb8e873e96abc59c46ebba168897b8dbd9a443af962df5

This is what I need.

There are two obvious drawbacks to this method. Firstly, I am doing RPC to get what should be locally available - somewhere. The second fact is that there is a race condition. If modules.get_current_instance_id() returns, say, 3, but instance # 2 is disabled between assigning this index to this process, and when I get an rpc response, I will be disabled by one of my rpc_result indexes.

How do I get this identifier in the application engine?

+6
source share
1 answer

You can get it from the environment variable INSTANCE_ID.

You can get it with:

 os.eniron["INSTANCE_ID"] 
0
source

Source: https://habr.com/ru/post/1015080/


All Articles