App Engine数据存储区中的GRPC和类型导入错误

时间:2018-03-28 06:23:58

标签: google-app-engine google-app-engine-python

我在App引擎中成功部署了我的应用程序。但是,当我尝试访问我的API时,收到以下消息:

  

错误:服务器错误   服务器遇到错误,无法完成您的请求。   请在30秒后再试一次。

我无法理解此问题的原因,因为它来自Datastore API。有人可以帮我解决这个问题吗?

以下是所有相关信息。

回溯

当引用URL的根

Before = Avoid,
Inside = Avoid,
After  = Always,
Master Page = None.

访问除root之外的API,向我显示另一个回溯

2018-03-27 08:08:45,090 <root> [ERROR]
2018-03-27 16:08:45.099 HKT
Traceback (most recent call last):
2018-03-27 16:08:45.099 HKT
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/6baee03590d046f1_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
2018-03-27 16:08:45.100 HKT
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
2018-03-27 16:08:45.100 HKT
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/6baee03590d046f1_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
2018-03-27 16:08:45.100 HKT
    handler, path, err = LoadObject(self._handler)
2018-03-27 16:08:45.100 HKT
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/6baee03590d046f1_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 96, in LoadObject
2018-03-27 16:08:45.100 HKT
    __import__(cumulative_path)
2018-03-27 16:08:45.100 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/web/app.py", line 6, in <module>
2018-03-27 16:08:45.100 HKT
    from web.routes import ROUTES_BLUEPRINT
2018-03-27 16:08:45.100 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/web/routes.py", line 7, in <module>
2018-03-27 16:08:45.100 HKT
    from metadata.repository.gcp_clouddatastore import GCPCloudDataStore
2018-03-27 16:08:45.100 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/metadata/repository/gcp_clouddatastore.py", line 8, in <module>
2018-03-27 16:08:45.100 HKT
    from google.cloud import datastore
2018-03-27 16:08:45.100 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/lib/google/cloud/datastore/__init__.py", line 60, in <module>
2018-03-27 16:08:45.100 HKT
    from google.cloud.datastore.batch import Batch
2018-03-27 16:08:45.100 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/lib/google/cloud/datastore/batch.py", line 24, in <module>
2018-03-27 16:08:45.100 HKT
    from google.cloud.datastore import helpers
2018-03-27 16:08:45.100 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/lib/google/cloud/datastore/helpers.py", line 29, in <module>
2018-03-27 16:08:45.100 HKT
    from google.cloud.datastore_v1.proto import datastore_pb2
2018-03-27 16:08:45.100 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/lib/google/cloud/datastore_v1/__init__.py", line 18, in <module>
2018-03-27 16:08:45.100 HKT
    from google.cloud.datastore_v1.gapic import datastore_client
2018-03-27 16:08:45.100 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/lib/google/cloud/datastore_v1/gapic/datastore_client.py", line 18, in <module>
2018-03-27 16:08:45.100 HKT
    import google.api_core.gapic_v1.client_info
2018-03-27 16:08:45.100 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/lib/google/api_core/gapic_v1/__init__.py", line 16, in <module>
2018-03-27 16:08:45.100 HKT
    from google.api_core.gapic_v1 import config
2018-03-27 16:08:45.100 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/lib/google/api_core/gapic_v1/config.py", line 23, in <module>
2018-03-27 16:08:45.100 HKT
    import grpc
2018-03-27 16:08:45.100 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/lib/grpc/__init__.py", line 22, in <module>
2018-03-27 16:08:45.100 HKT
    from grpc._cython import cygrpc as _cygrpc
2018-03-27 16:08:45.100 HKT
ImportError: dynamic module does not define init function (initcygrpc)
2018-03-27 16:08:45.111 HKT
This request caused a new process to be started for your application, and thus caused your application code to be loaded for the first time. This request may thus take longer and use more CPU than a typical request for your application.

app.yaml文件内容

2018-03-27 16:13:41.378 HKT
2018-03-27 08:13:41,377 <root> [ERROR] 
2018-03-27 16:13:41.378 HKT
Traceback (most recent call last):
2018-03-27 16:13:41.378 HKT
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/6baee03590d046f1_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
2018-03-27 16:13:41.378 HKT
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
2018-03-27 16:13:41.378 HKT
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/6baee03590d046f1_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
2018-03-27 16:13:41.378 HKT
    handler, path, err = LoadObject(self._handler)
2018-03-27 16:13:41.378 HKT
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/6baee03590d046f1_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 96, in LoadObject
2018-03-27 16:13:41.378 HKT
    __import__(cumulative_path)
2018-03-27 16:13:41.378 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/web/app.py", line 6, in <module>
2018-03-27 16:13:41.378 HKT
    from web.routes import ROUTES_BLUEPRINT
2018-03-27 16:13:41.378 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/web/routes.py", line 7, in <module>
2018-03-27 16:13:41.378 HKT
    from metadata.repository.gcp_clouddatastore import GCPCloudDataStore
2018-03-27 16:13:41.378 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/metadata/repository/gcp_clouddatastore.py", line 8, in <module>
2018-03-27 16:13:41.378 HKT
    from google.cloud import datastore
2018-03-27 16:13:41.378 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/lib/google/cloud/datastore/__init__.py", line 60, in <module>
2018-03-27 16:13:41.378 HKT
    from google.cloud.datastore.batch import Batch
2018-03-27 16:13:41.378 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/lib/google/cloud/datastore/batch.py", line 24, in <module>
2018-03-27 16:13:41.378 HKT
    from google.cloud.datastore import helpers
2018-03-27 16:13:41.378 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/lib/google/cloud/datastore/helpers.py", line 29, in <module>
2018-03-27 16:13:41.378 HKT
    from google.cloud.datastore_v1.proto import datastore_pb2
2018-03-27 16:13:41.378 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/lib/google/cloud/datastore_v1/__init__.py", line 17, in <module>
2018-03-27 16:13:41.378 HKT
    from google.cloud.datastore_v1 import types
2018-03-27 16:13:41.378 HKT
ImportError: cannot import name types

appengine_config.py文件内容

runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: web.app.app
libraries:
- name: lxml
  version: latest

3 个答案:

答案 0 :(得分:3)

您正在使用数据存储客户端库,但这在标准环境中不起作用。来自docs

  

您无法在App Engine标准环境中将Cloud Datastore客户端库与Python应用程序一起使用。

您需要按照文档中的建议使用ndb包(我相信还有一个REST API,但我从未使用过它)。

答案 1 :(得分:1)

第一次回溯似乎暗示grpc以某种方式参与,这是一个问题。根据GAE标准环境尚不支持issue 149 grpc的评论。

但是,由于您使用的是GAE标准环境,因此您应该使用推荐的GAE优化(以及功能更丰富的)NDB Client Library来访问数据存储区,而不是通用的google.cloud.datastore(哪些可能确实包含不符合标准env沙箱限制的功能。)

答案 2 :(得分:0)

Google以某种方式在2019年解决了该问题,以便您在Standard上运行的Python 2.7应用现在可以使用google.cloud.datastore库。要消除grpc错误,请将这些行添加到libraries文件的app.yaml部分中(或添加该行,如果尚不存在的话):

libraries:
- name: grpcio
  version: 1.0.0

接受的答案中的说法:“您不能在App Engine标准环境中将Cloud Datastore客户端库与Python应用程序一起使用”,这不再是正确的,并且已从文档中删除。

如果从Cloud Datastoregoogle.appengine.ext.ndb)移植到the App Engine NDB library,首先移植到the Cloud NDB librarygoogle.cloud.ndb可能会更容易, Cloud Datastore的“ NDB客户端库”。在this migration page上描述了将grpcio引用添加到app.yaml的步骤。

使用此中间端口的另一个原因是,Cloud NDB在Python 3中也可用,这使您有机会将应用程序从2.x迁移到3.x,并且仍然可以正常工作。另外,您还获得了使用App Engine第二代运行时的额外好处(Gen1 std仅支持2.7,而Flex支持2.7或3.x,而Gen2仅支持3.x。)

最后,从那里连接到Cloud Datastoregoogle.cloud.datastore)。中间步骤基本上可以使您的“应用程序基础架构”为Cloud Datstore做好准备,而您的大部分工作将是将NDB数据CRUD迁移到Cloud Datastore,但不必担心S / b会解决的这些库问题。以上中间步骤。 (我将尝试对所有这些内容进行书面记录,并在我得到一些答案后更新此答案。)