Google Cloud Pub/Sub SDK - subscribe() 至少需要 3 个参数(给定 2 个)
Google Cloud Pub/Sub SDK - subscribe() takes at least 3 arguments (2 given)
下面的代码,用于订阅主题,几个月前就可以使用了。
from google.cloud import pubsub
def callback(message):
print(message.data)
message.ack()
project_id = "my_project"
topic_name = "xxx"
subscription_name = "xxx"
subscriber = pubsub.SubscriberClient()
topic = "projects/{}/topics/{}".format(project_id, topic_name)
subscription_name = 'projects/{}/subscriptions/{}'.format(project_id, subscription_name)
subscription = subscriber.subscribe(subscription_name)
future = subscription.open(callback)
try:
future.result()
except Exception as ex:
subscription.close()
raise
我现在尝试 运行 它,但收到以下错误消息:
File "pubsub_sub.py", line 16, in < module >
subscription = subscriber.subscribe(subscription_name)
TypeError: subscribe() takes at least 3 arguments (2 given)
正如 Google-Cloud Pub/Sub 文档中所述,我的代码似乎是正确的。另外,正如我所说,同样的代码在过去是有效的。有什么建议吗?
如 the docs 中所述,您缺少回调。
看起来确实像 README.rst on Github is wrong, since on the code 我们可以看到这 3 个参数是必需的:
def subscribe(
self, subscription, callback, flow_control=(),
scheduler=None):
我刚刚向文件提出了 fix 以供澄清。
编辑:
关于它以前工作的事实,您可以在回购中看到如何拉动 5237 subscribe_experimental was created, and this method required the callback, unlike the regular subscribe. Later on, on pull 5274,subscribe_experimental 被提升为订阅,这意味着现在需要回调。
如果您在提供回调时遇到错误,请确保您的所有库都是最新的。
下面的代码,用于订阅主题,几个月前就可以使用了。
from google.cloud import pubsub
def callback(message):
print(message.data)
message.ack()
project_id = "my_project"
topic_name = "xxx"
subscription_name = "xxx"
subscriber = pubsub.SubscriberClient()
topic = "projects/{}/topics/{}".format(project_id, topic_name)
subscription_name = 'projects/{}/subscriptions/{}'.format(project_id, subscription_name)
subscription = subscriber.subscribe(subscription_name)
future = subscription.open(callback)
try:
future.result()
except Exception as ex:
subscription.close()
raise
我现在尝试 运行 它,但收到以下错误消息:
File "pubsub_sub.py", line 16, in < module >
subscription = subscriber.subscribe(subscription_name)
TypeError: subscribe() takes at least 3 arguments (2 given)
正如 Google-Cloud Pub/Sub 文档中所述,我的代码似乎是正确的。另外,正如我所说,同样的代码在过去是有效的。有什么建议吗?
如 the docs 中所述,您缺少回调。
看起来确实像 README.rst on Github is wrong, since on the code 我们可以看到这 3 个参数是必需的:
def subscribe(
self, subscription, callback, flow_control=(),
scheduler=None):
我刚刚向文件提出了 fix 以供澄清。
编辑:
关于它以前工作的事实,您可以在回购中看到如何拉动 5237 subscribe_experimental was created, and this method required the callback, unlike the regular subscribe. Later on, on pull 5274,subscribe_experimental 被提升为订阅,这意味着现在需要回调。
如果您在提供回调时遇到错误,请确保您的所有库都是最新的。