简体   繁体   English

为什么 dev_appserver.py 没有抛出错误就退出了?

[英]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.我也不知道数据库当前存储在哪里。

  1. It should be count() and not count ie它应该是count()而不是count

    logging.critical('count is: %s' % e1.count())

  2. 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))
  3. 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.在尝试使用键之前,您必须修改代码以首先检查您是否具有e1e2的值。

  4. 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...??可能是版本的问题……??

  5. 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

  6. 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.

相关问题 dev_appserver.py BadArgumentError: app 不能为空 - dev_appserver.py BadArgumentError: app must not be empty `vendor.add` 不会将依赖项暴露给 dev_appserver.py - `vendor.add` doesn't expose dependencies to dev_appserver.py Python3 dev_appserver 和数据存储 - Python3 dev_appserver and datastore 为 dev_appserver 导入 python 模块的正确方法是什么? - What is the proper way import python modules for dev_appserver? 为什么 Android 不退出打盹模式,尽管 FCM 中消息的优先级很高? - Why Android does not exit DOZE mode, despite the high priority of the message in FCM? 为什么这个 Dataflow 管道运行没有错误,但不读取任何数据? - Why does this Dataflow pipeline run with no errors, but doesn't read any data? GITLAB CI 加载密钥“/dev/fd/63”时出错:格式无效错误:作业失败:退出代码 1 - GITLAB CI Error loading key "/dev/fd/63": invalid format ERROR: Job failed: exit code 1 为什么零旗存在? - Why does the Zero Flag exist? Firebase 以非零退出代码(项目路径中的空格)开头的部署错误 - Firebase deploy errors starting with non-zero exit code (space in project path) 当我尝试初始化 firebase 应用程序时,为什么 Google Cloud Functions 会抛出“配置 firebase.databaseURL 的无效值”错误? - Why is Google Cloud Functions throwing a "Invalid value for config firebase.databaseURL" error when I try to initialize a firebase app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM