简体   繁体   English

如何摆脱类型错误?

[英]How to get rid of TypeError?

Currently working on implementing Google Cloud storage on Raspberry Pi.目前致力于在 Raspberry Pi 上实现 Google Cloud 存储。 Whenever I run my code, I get the error:每当我运行我的代码时,我都会收到错误消息:

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

This is my code:这是我的代码:

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")

Any advice on how to solve said issue?关于如何解决上述问题的任何建议? :) :)

The callback function set by "add_done_callback" is called with an argument (the future itself), but it is defined without any.由“add_done_callback”设置的回调函数是用一个参数(未来本身)调用的,但它没有任何定义。

Changing your callback creation to:将您的回调创建更改为:

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

should solve the problem.应该解决问题。

See: 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请参阅: 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM