Sni*_*gus 6 python google-app-engine google-app-engine-python
我可以通过调用获得正在运行的进程的短整数instance_id modules.get_current_instance_id(),但这会将整数索引返回到当前版本的应用程序的实例列表中.我可以创建一个rpc来获取完整的十六进制instance_id,如下所示:
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())]
Run Code Online (Sandbox Code Playgroud)
这给了我一个dict,其中包含一个'id'键值对,其中值看起来像这样:00c61b117cb3c50973d6a73225b3d807eb8e873e96abc59c46ebba168897b8dbd9a443af962df5
这就是我需要的.
这种方法有两个明显的缺点.首先是我正在做RPC以获得必须在本地可用的东西 - 某处.第二个是存在竞争条件的事实.如果modules.get_current_instance_id()返回,比如说3,但实例#2在为该进程分配索引之间关闭,当我得到rpc响应时,我将在我的索引中关闭rpc_result.
如何在应用引擎中获取此ID?
您应该能够从 INSTANCE_ID 环境变量中获取它。
您可以通过以下方式获取它:
os.eniron["INSTANCE_ID"]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
139 次 |
| 最近记录: |