gpt4 book ai didi

python - 获取错误类型错误 : create_task() takes from 1 to 2 positional arguments but 3 were given while creating google cloud tasks

转载 作者:行者123 更新时间:2023-12-01 23:47:53 29 4
gpt4 key购买 nike

from google.cloud import tasks_v2

import json


GCP_PROJECT='test'
GCP_LOCATION='europe-west6'


def enqueue_task(queue_name, payload, process_url):
client = tasks_v2.CloudTasksClient()
parent = client.queue_path(GCP_PROJECT, GCP_LOCATION, queue_name)

task = {
'app_engine_http_request': {
'http_method': 'POST',
'relative_uri': process_url
}
}

if payload is None:
return False

payload = json.dumps(payload)
converted_payload = payload.encode()

task['app_engine_http_request']['body'] = converted_payload
return client.create_task(parent, task)

当我尝试创建 Google Cloud Task 时,我不断收到以下错误。使用的 Google App Engine 运行时是 python38。它工作正常,但在使用 gcp CLI 部署后突然它现在不工作了。

Traceback (most recent call last):
File "/layers/google.python.pip/pip/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/layers/google.python.pip/pip/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/layers/google.python.pip/pip/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/layers/google.python.pip/pip/flask/_compat.py", line 39, in reraise
raise value
File "/layers/google.python.pip/pip/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/layers/google.python.pip/pip/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/srv/main.py", line 153, in sync_request
queue_response = queues.enqueue_task(
File "/srv/queues.py", line 28, in enqueue_task
return client.create_task(parent, task)
TypeError: create_task() takes from 1 to 2 positional arguments but 3 were given

最佳答案

同样的事情刚刚发生在我身上(在实时服务器上:/)。事实证明,Cloud Tasks 库在 2.0.0 版本中发生了重大变化。你可以阅读what you need to do to upgrade here .

您的问题出在这一行:

client.create_task(parent, task)

更新后的库需要使用字典作为位置参数或使用关键字参数。所以这应该解决它:

client.create_task(parent=parent, task=task)

编辑:既然我已经为自己完成了这项工作,请查看您的代码,您还必须更改以下内容:

# Before
parent = client.queue_path(GCP_PROJECT, GCP_LOCATION, queue_name)
# After
parent = client.queue_path(project=GCP_PROJECT, location=GCP_LOCATION, queue=queue_name)

# Before
'http_method': 'POST',
# After
'http_method': tasks_v2.HttpMethod.POST,

关于python - 获取错误类型错误 : create_task() takes from 1 to 2 positional arguments but 3 were given while creating google cloud tasks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63920923/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com