dev_appserver.py无报错直接退出?AppEngine本地服务异常排查
App Engine Python 2服务启动异常排查与Datastore相关疑问
问题描述
执行dev_appserver.py app.yaml后,程序输出日志后无报错直接退出,未在8000端口启动服务;但添加--support_datastore_emulator=False参数后可正常运行。
相关日志
/home/username/google-cloud-sdk/lib/third_party/google/auth/crypt/_cryptography_rsa.py:22: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in the next release. import cryptography.exceptions INFO 2022-12-20 11:59:41,931 devappserver2.py:239] Using Cloud Datastore Emulator. We are gradually rolling out the emulator as the default datastore implementation of dev_appserver. If broken, you can temporarily disable it by --support_datastore_emulator=False Read the documentation: https://cloud.google.com/appengine/docs/standard/python/tools/migrate-cloud-datastore-emulator INFO 2022-12-20 11:59:41,936 devappserver2.py:316] Skipping SDK update check. INFO 2022-12-20 11:59:42,332 datastore_emulator.py:156] Starting Cloud Datastore emulator at: http://localhost:22325 INFO 2022-12-20 11:59:42,981 datastore_emulator.py:162] Cloud Datastore emulator responded after 0.648865 seconds INFO 2022-12-20 11:59:42,982 <string>:384] Starting API server at: http://localhost:38915
服务代码
import webapp2 import datetime from google.appengine.ext import db, deferred, ndb import uuid from base64 import b64decode, b64encode import logging class Email(ndb.Model): email = ndb.StringProperty() class DB(webapp2.RequestHandler): def post(self): try: mail = Email() mail.email = 'Test' mail.put() except Exception as e: print(e) self.response.headers['Content-Type'] = 'application/json; charset=utf-8' return self.response.out.write(e) def get(self): try: e1 = Email.query() logging.critical('count is: %s' % e1.count) e1k = e1.get(keys_only=True) logging.critical('count 2 is: %s' % e1k.count) e1 = e1.get() key = unicode(e1.key.urlsafe()) logging.critical('This is a critical message: %s' % key) logging.critical('This is a critical message: %s' % e1k) e2 = ndb.Key(urlsafe=key).get() self.response.headers['Content-Type'] = 'application/json; charset=utf-8' return self.response.out.write(str(e2.email)) except Exception as e: print(e) self.response.headers['Content-Type'] = 'application/json; charset=utf-8' return self.response.out.write(e) app = webapp2.WSGIApplication([ ('/', DB) ], debug=True)
问题排查步骤
- 检查Datastore模拟器依赖:Datastore模拟器基于Java开发,确认本地已安装符合SDK要求的Java运行环境。
- 开启详细日志定位问题:添加
--log_level=debug参数启动服务,查看更详细的日志输出,定位模拟器启动失败的具体原因。 - 验证端口占用:使用
lsof -i :22325(Linux/macOS)或netstat -tulpn | grep 22325命令,检查模拟器使用的22325端口是否被其他进程占用。 - 单独测试模拟器:执行
gcloud beta emulators datastore start单独启动模拟器,确认其能否正常运行,再测试数据读写功能,判断是否为模拟器本身的问题。 - 排查SDK兼容性:Python 2已停止官方支持,当前SDK版本可能对Python 2的Datastore模拟器存在兼容问题,可尝试降级到2022年中期的SDK版本,或升级到最新兼容版本(注意Python 2的支持限制)。
--support_datastore_emulator=False参数说明与存储位置
参数作用
该参数用于关闭新版Cloud Datastore模拟器,切换回旧版的本地Datastore实现(基于SQLite的本地存储)。Google正在逐步将Datastore模拟器作为dev_appserver的默认实现,旧版实现会被逐步淘汰。
存储位置
- 禁用模拟器时,数据存储在本地SQLite数据库文件中,默认路径:
- Linux/macOS:
~/.config/gcloud/emulators/datastore/local_db.bin(部分旧版SDK可能存储在项目目录下的.gae/local_db.bin) - Windows:
%APPDATA%\gcloud\emulators\datastore\local_db.bin
- Linux/macOS:
- 启用模拟器时,数据默认存储在临时目录,模拟器停止后数据会被清除;若需持久化数据,启动时需添加
--data-dir指定存储目录。
内容的提问来源于stack exchange,提问作者Yusuf Avcı
相关产品推荐
相关产品推荐

