使用 Python3 使用 GAE 使用 Cloud Tasks 和 Cloud Datastore 进行本地开发

Local development with Cloud Tasks & Cloud Datastore with GAE with Python3

Context:- 我们正在使用 GAEPython3 等等GAE API 包不可用,因此我们使用 google-cloud-* packages 与 GAE 服务交互

google-cloud-tasks用于推送队列,google-数据存储的云数据存储

问题:- 无法在开发环境中进行测试,因为 google-cloud-* 包直接作用于生产服务。
如果我使用 google-cloud-tasks 推送任务,它将推送到生产队列中,类似地,如果我从开发环境中创建或更新实体,它将更新实体生产数据存储。

早期在本地系统中使用 GAE API 包时,它曾经有用于开发目的的本地云任务和数据存储。

我认为这是一个非常普遍的大问题,我想知道是否其他人也遇到过这样的问题并找到了解决方案。

对于 Cloud Datastore,您可以按照 https://cloud.google.com/datastore/docs/tools/datastore-emulator 中的说明使用本地模拟器而不是生产 Datastore 数据库。

https://cloud.google.com/tasks/docs/migrating 中所述,模拟器目前不支持 Cloud Tasks。

这个 Google Cloud Tasks 的本地模拟器对我有用。

pip install gcloud-tasks-emulator
gcloud-tasks-emulator start --port=9090

// 注意-:默认情况下是 gcloud-tasks-emulator 命令 不可用全局切换安装目录。

/Users/{userName}/Library/Python/3.7/bin
./gcloud-tasks-emulator start --port=9090

现在我们可以添加代码更改以支持本地环境中的云任务。

import grpc
from google.cloud.tasks_v2 import CloudTasksClient
from google.cloud.tasks_v2.gapic.transports.cloud_tasks_grpc_transport import C 
loudTasksGrpcTransport

client = CloudTasksClient(
 transport=CloudTasksGrpcTransport
 (channel=grpc.insecure_channel("127.0.0.1:9090"))
)

访问此 link 以获得完整说明 https://pypi.org/project/gcloud-tasks-emulator/

我为 Python 开发构建了一个 in-process emulator

另请参阅在本地主机的单独进程中 运行 的一些模拟器:Potato London 的 gcloud-tasks-emulator, mentioned in the answer above, and Aert van de Hulsbeek’s cloud-tasks-emulator