google-app-engine - Google Cloud 任务和 Google App Engine Python 3

标签 google-app-engine google-cloud-tasks

我正在尝试与 Google Cloud Tasks API 合作

在 python2.7 应用程序引擎标准中,您拥有这个令人惊叹的库( deferred ),它允许您轻松地将工作人员分配给可以异步完成的多个任务。

所以在 webapp2 处理程序中我可以这样做:

create_csv_file(data):
  #do a bunch of work ...
  return

MyHandler(webapp2.Handler):
  def get(self)
    data = myDB.query()
    deferred.defer(create_csv_file, data)

现在我正在开发新的 Google App Engine Python 3 运行时,并且延迟库不适用于 GAE Py3。

谷歌云任务是正确的解决方案/替代品吗?

这就是我现在所处的位置...我已经在互联网上搜索寻找答案,但我的 Google 能力让我失败了。我找到了一些示例,但它们不是很好,而且看起来好像您应该从 gcloud 控制台或本地创建/添加任务,但没有从前端 api 端点添加任务的示例。

ExportCSVFileHandler(Resource):
  def get(self):
    create_task()
    return 

CSVTaskHandler(Resource):
  def(post):
    #do a lot of work creating a csv file
    return

create_task():
    client = tasks.CloudTasksClient(credentials='mycreds')
    project = 'my-project_id'
    location = 'us-east4'
    queue_name = 'csv-worker'

    parent = client.location_path(project, location)

    the_queue = {
        'name': client.queue_path(project, location, queue_name),
        'rate_limits': {
            'max_dispatches_per_second': 1
        },
        'app_engine_routing_override': {
            'version': 'v2',
            'service': 'task-module'
        }
    }

    queues = [the_queue]
    task = {
        'app_engine_http_request': {
            'http_method': 'GET',
            'relative_uri': '/create-csv',
            'app_engine_routing': {
                'service': 'worker'
            },
            'body': str(20).encode()
        }
    }

    # Use the client to build and send the task.
    response = client.create_task(parent, task)

    print('Created task {}'.format(response.name))

    # [END taskqueues_using_yaml]
    return response

最佳答案

是的,Cloud Tasks 是 App Engine 任务队列的替代品。该 API 可以从任何地方调用,即本地调用、App Engine 调用、外部服务调用,甚至 gcloud 调用。这些示例向您展示了如何在本地执行此操作,但您可以轻松地将旧的任务队列代码替换为新的 Cloud Tasks 库。

不幸的是,云任务没有延迟库。有多种方法可以解决这个问题。为任务处理程序创建单独的端点,并使用 App Engine 路由将任务发送到正确的端点,或将元数据添加到任务正文,以便您的处理程序适当处理任务请求。

关于google-app-engine - Google Cloud 任务和 Google App Engine Python 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57661193/

相关文章:

java - 为什么我在使用 Google App Engile 的端点插入数据时在 Android 中出现错误?

java - 从使用 EJB 的 Glassfish 迁移到使用 Spring 的 Google App Engine

python - 在 ndb 对象中动态设置属性

java - Google App Engine 项目可以在开发环境中运行,但在部署时不能运行

python - 从谷歌云功能中排队大量任务

python - 从 Compute Engine 与 App Engine 通信的安全方式

php - 如何从 HTTP 请求验证 Google Cloud Task token ?

java - 向 Google Cloud 任务添加参数

service-accounts - Google Cloud Tasks 无法对 Cloud Run 进行身份验证

php - 使用任务队列处理程序时谷歌云 SQL 实例中的数据库连接超时