简体   繁体   English

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

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

I get the following error when running scrapeBackend.py运行 scrapeBackend.py 时出现以下错误

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

I am trying to use the send_topic method inherited from the ProducerConsumer class in customKafka.py我正在尝试使用从 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 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"))

Looks like the getProducer() is returning None however it should return something since I initaliazed before running看起来 getProducer() 正在返回 None 但是它应该返回一些东西,因为我在运行之前进行了初始化

The issue is in your __init__ function of parent class.问题出在父类的__init__函数中。

It should be implemented like this:它应该像这样实现:

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)

Notice the difference in the if statements.注意 if 语句的区别。 Hope this helps!!希望这可以帮助!!

you should call super() in your __init__ of the subclass then, you should be able to call the function send_topic from the object scrap1您应该在子类的__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)

and your __init__ of ProductConsumer can't deal with input parameters (the 2 if-conditions), it should be implemented like this:并且您的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.

相关问题 为什么我会收到“AttributeError: 'NoneType' object has no attribute 'get'” - why am i getting "AttributeError: 'NoneType' object has no attribute 'get' " 为什么我收到“AttributeError: 'NoneType' object has no attribute 'send'”错误 - Why am I getting a `AttributeError: 'NoneType' object has no attribute 'send'` Error 为什么我在 /admin/main/consultation/'NoneType' object 没有属性 'lastname' 处出现 AttributeError 错误 - why am i getting error of AttributeError at /admin/main/consultation/ 'NoneType' object has no attribute 'lastname' Web Scraper:为什么会出现AttributeError:'NoneType'对象没有属性'text'? - Web Scraper: Why am I getting AttributeError: 'NoneType' object has no attribute 'text'? 为什么我收到 AttributeError: “'NoneType' object has no attribute 'get'” 与 Python 和 Tkinkter? - Why I am getting AttributeError: “'NoneType' object has no attribute 'get'” with Python and Tkinkter? 为什么我在执行 PCA 时遇到此错误:- AttributeError: 'NoneType' object has no attribute 'split' - Why i am getting this error while doing PCA :- AttributeError: 'NoneType' object has no attribute 'split' 我该如何修复,AttributeError: 'NoneType' 对象没有属性 'send' - How can I fix, AttributeError: 'NoneType' object has no attribute 'send' 为什么我得到“NoneType” object 没有属性“哔”? - Why am I getting 'NoneType' object has no attribute 'beep'? 为什么我在 .kv 文件中得到“NoneType”对象没有属性? - Why am I getting `'NoneType' object has no attribute` in .kv file? 为什么我收到 'AttributeError: 'NoneType' object has no attribute 'get_screenshot_as_file' ? 显示完整代码 Pycharm/Pytest - Why am I getting 'AttributeError: 'NoneType' object has no attribute 'get_screenshot_as_file' ? full code shown Pycharm/Pytest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM