我正在尝试使用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。
谷歌云任务是正确的解决方案/替代品吗?
这就是我现在所处的位置......我已经在互联网上搜寻答案,但我的谷歌能力让我失望了。我找到了一些示例,但它们不是很好,它们看起来好像您应该从 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