繁体   English   中英

为什么我会收到 AttributeError: 'NoneType' object has no attribute 'send' 以及如何避免它?

[英]Why am I getting AttributeError: 'NoneType' object has no attribute 'send' and how to avoid it?

运行 scrapeBackend.py 时出现以下错误

AttributeError: 'NoneType' object has no attribute 'send'

我正在尝试使用从 customKafka.py 中的 ProducerConsumer 类继承的 send_topic 方法

import time
import pymongo
from pymongo import MongoClient
from customKafka import ProducerConsumer


class scrapeBackend(ProducerConsumer):

    def run(self):
        ARTICLE_SCRAPED_KAFKA_TOPIC = "raw_text"
        ARTICLE_LIMIT = 20
        print("Generating raw article")
        print("generating every 3 seconds")

        for i in range(1,ARTICLE_LIMIT):
            data={
                "article_id": i,
                "source" : "bbc",
                "article" : "this is an article"
            }
            super().send_topic("raw_text",data)
            #collection.insert_one(data)
            print(f"done sending {i}")
            time.sleep(3)

scrap1=scrapeBackend(True,False,"raw_text","localhost:29092")

scrap1.run()

customKafka.py

from kafka import KafkaConsumer
from kafka import KafkaProducer
import json
class ProducerConsumer:
    __isProducer=None
    __isConsumer=None
    __producer=None
    __consumer=None

    def __init__(self,isProducer,isConsumer,topic,bootstrap_servers):
        if (self.__isProducer):
            self.__producer=KafkaProducer(bootstrap_servers=bootstrap_servers)
        if (self.__isConsumer):
            self.__consumer= KafkaConsumer(
                topic,
                bootstrap_servers=bootstrap_servers,
                auto_offset_reset='earliest',
                enable_auto_commit=True,
                consumer_timeout_ms = 10000)

    def get_producer(self):
        return self.__producer

    def get_consumer(self):
        return self.__consumer

    def send_topic(self,topic,data):
        self.get_producer().send(topic, json.dumps(data).encode("utf-8"))

看起来 getProducer() 正在返回 None 但是它应该返回一些东西,因为我在运行之前进行了初始化

问题出在父类的__init__函数中。

它应该像这样实现:

def __init__(self,isProducer,isConsumer,topic,bootstrap_servers):
        if isProducer:
            self.__producer=KafkaProducer(bootstrap_servers=bootstrap_servers)
        if isConsumer:
            self.__consumer= KafkaConsumer(
                topic,
                bootstrap_servers=bootstrap_servers,
                auto_offset_reset='earliest',
                enable_auto_commit=True,
                consumer_timeout_ms = 10000)

注意 if 语句的区别。 希望这可以帮助!!

您应该在子类的__init__中调用super()然后,您应该能够从对象scrap1调用函数send_topic

class scrapeBackend(ProducerConsumer):
    def __init__(self, *args, **kwargs):
        super(scrapeBackend, self).__init__(*args, **kwargs)

    def run(self):
        ARTICLE_SCRAPED_KAFKA_TOPIC = "raw_text"
        ARTICLE_LIMIT = 20
        print("Generating raw article")
        print("generating every 3 seconds")

        for i in range(1,ARTICLE_LIMIT):
            data={
                "article_id": i,
                "source" : "bbc",
                "article" : "this is an article"
            }
            self.send_topic("raw_text", data)
            #collection.insert_one(data)
            print(f"done sending {i}")
            time.sleep(3)

并且您的ProductConsumer__init__无法处理输入参数(2个 if 条件),它应该像这样实现:

class ProducerConsumer:
    def __init__(self,isProducer,isConsumer,topic,bootstrap_servers):
        if isProducer:
            self.__producer = KafkaProducer(bootstrap_servers=bootstrap_servers)
        if isConsumer:
            self.__consumer = KafkaConsumer(
                topic,
                bootstrap_servers=bootstrap_servers,
                auto_offset_reset='earliest',
                enable_auto_commit=True,
                consumer_timeout_ms = 10000)

    def get_producer(self):
        return self.__producer

    def get_consumer(self):
        return self.__consumer

    def send_topic(self,topic,data):
        self.get_producer().send(topic, json.dumps(data).encode("utf-8"))

暂无
暂无

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

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