[英]Why does dev_appserver.py exit without throwing errors?
I am trying to run a simple python 2 server code with AppEngine and Datastore.我正在尝试使用 AppEngine 和 Datastore 运行一个简单的 python 2 服务器代码。 When I run
dev_appserver.py app.yaml
, the program immediately exits (without an error) after the following outputs:当我运行
dev_appserver.py app.yaml
时,程序在以下输出后立即退出(没有错误):
/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
Ideally, it should have continued by runnning the server on port 8000. Also, it works with option --support_datastore_emulator=False
.理想情况下,它应该通过在端口 8000 上运行服务器来继续。此外,它可以使用选项
--support_datastore_emulator=False
。
This is the code:这是代码:
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)
How can I find the reason this is not working?我怎样才能找到这不起作用的原因?
Edit: I figured that the dev server works and writes to a datastore even with support_datastore_emulator=False
option.编辑:我认为即使使用
support_datastore_emulator=False
选项,开发服务器也可以工作并写入数据存储。 I am confused by this option.我对这个选项感到困惑。 I also don't know where the database is stored currently.
我也不知道数据库当前存储在哪里。
It should be count()
and not count
ie它应该是
count()
而不是count
即
logging.critical('count is: %s' % e1.count())
A get
returns only 1 record and so it doesn't make sense to do a count after calling a get
. get
仅返回 1 条记录,因此在调用get
后进行计数没有意义。 Besides, the count
operation is a method of the query instance not the results.此外,
count
操作是查询实例的一种方法,而不是结果。 This means the following code is incorrect这意味着下面的代码是不正确的
e1k = e1.get(keys_only=True) logging.critical('count 2 is: %s' % e1k.count)
You should replace it with您应该将其替换为
elk = e1.fetch(keys_only=True) # fetch gives an array logging.critical('count 2 is: %s' % len(e1k))
When you first run your App, it will execute the GET part of your code and because this is the first time your App is being run, you have no record in Datastore.当你第一次运行你的应用程序时,它会执行你代码的 GET 部分,因为这是你的应用程序第一次运行,你在数据存储中没有记录。 This means
e1 = e1.get()
will return None and key = unicode(e1.key.urlsafe())
will lead to an error.这意味着
e1 = e1.get()
将返回 None 并且key = unicode(e1.key.urlsafe())
将导致错误。
You have to modify your code to first check you have a value for e1
or e2
before you attempt to use the keys.在尝试使用键之前,您必须修改代码以首先检查您是否具有
e1
或e2
的值。
I ran your code with dev_appserver.py and it displayed these errors for me in the logs.我用 dev_appserver.py 运行了你的代码,它在日志中为我显示了这些错误。 But I ran it with an older version of gcloud SDK (Google Cloud SDK 367.0.0).
但我使用旧版本的 gcloud SDK (Google Cloud SDK 367.0.0) 运行它。 I don't know why yours exited without displaying any errors.
我不知道为什么你的退出没有显示任何错误。 Maybe it's due to the version...??
可能是版本的问题……??
Separately - Don't know why you're importing db
.另外 - 不知道你为什么要导入
db
。 Google moved on to ndb
long ago and you don't use db
in your code谷歌很久以前就转向了
ndb
而你没有在你的代码中使用db
The default datastore (for the older generation runtimes like Python 2) is in .config (hidden folder) > gcloud > emulators > datastore
默认数据存储(对于老一代运行时,如 Python 2)位于
.config (hidden folder) > gcloud > emulators > datastore
You can also specify your own location by using the flag --datastore_path
.您还可以使用标志
--datastore_path
指定您自己的位置。 See documentation请参阅文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.