Go 任务队列示例
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本示例创建一个显示 HTML 表单的应用。在对话框中输入一个字符串,然后点击 Add
。接着该应用就会计算您以这种方式输入字符串的次数。
该应用执行以下操作:
- 当您点击
Add
时,表单使用 HTTP POST
请求将字符串发送到在 App Engine 上运行的应用。接着,应用将字符串捆绑到任务中并将其发送到默认队列。
- 队列将任务转发到内置的任务处理程序,映射到网址
/worker
,后者将字符串异步写入数据存储区。
- 发送 HTTP
GET
请求会显示您输入的字符串列表以及 Add
每个字符串的次数,具体操作是键入请求或在下拉框中点击请求。
要将此应用部署到 App Engine,请执行以下操作:
将以下内容复制到名为 queue.yaml
的文件中。这会将处理任务的速率从默认的每秒 5 次更改为每秒 3 次。
queue:
- name: default
rate: 3/s
在同一目录中,将以下内容复制到您命名的文件(以 .go
结尾)中。这是应用代码,包括任务处理程序。
在同一目录中,将以下内容复制到名为 app.yaml
的文件中。这会为 App Engine 配置您的应用:
确保您已有部署了 App Engine 应用的 Google Cloud Platform 项目,而且您已初始化并配置了该项目的 gcloud
命令。
使用 gcloud app deploy
命令将应用部署到 App Engine。
使用 gcloud app browse
命令查看应用的运行情况。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-03-26。"],[[["This application counts and displays the number of times a string is entered via an HTML form."],["When a string is added, it's sent to App Engine via an HTTP POST request, bundled into a task, and added to the default queue."],["The task queue forwards the task to a worker handler, which then writes the string to a datastore asynchronously."],["An HTTP GET request retrieves and displays a list of the entered strings and their corresponding counts."],["The application can be deployed to App Engine using specific configuration files and the `gcloud` command-line tool."]]],[]]