如何摆脱TypeError?



目前正在树莓派上实现谷歌云存储。每当我运行我的代码,我得到错误:

TypeError: callback() takes 0 positional arguments but 1 was given

这是我的代码

from google.cloud import pubsub_v1
import json
import datetime
import time
project_id = "projectid"
topic_name = "topic"
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(project_id, topic_name)
futures = dict()
def get_callback(f, data):
def callback():
try:
futures.pop(data)
except:
print("Please handle {} for {}.".format(f.exception(), data))
return callback
while True:
time.sleep(3)
text = "Hello Google"
data = {"text":text}
print(data)

future = publisher.publish(topic_path, data=(json.dumps(data)).encode("utf-8"))
future.add_done_callback(get_callback(future, data))
time.sleep(5)
while futures:
time.sleep(5)

print("Published message with error handler")

对于如何解决这个问题有什么建议吗?:)

add_done_callback"设置的回调函数被调用时带一个参数(未来本身),但它的定义没有任何参数。

将您的回调创建更改为:

def get_callback(f, data):
def callback(future):
...

应该能解决这个问题。

看:https://cloud.google.com/python/docs/reference/pubsub/latest/google.cloud.pubsub_v1.publisher.futures.Future google_cloud_pubsub_v1_publisher_futures_Future_add_done_callback

相关内容

  • 没有找到相关文章

最新更新