简体   繁体   English

Kafka Python,如何跟踪消费者在不同的过程中开始

[英]Kafka Python, How to track consumer started in a different process

I am fairly new to Python and just getting started with Kafka, so pardon my terminologies if I'm wrong somewhere. 我对Python很新,只是开始使用Kafka,所以请原谅我的术语,如果我在某处错了。

So I have a Django based web application, where I am sending a json messages through Kafka Producer within the same process. 所以我有一个基于Django的Web应用程序,我在同一个进程中通过Kafka Producer发送json消息。 However while creating a topic pragmatically, I am also starting(subscribing) a new consumer in a separate Process for that particular topic. 然而,在以实用方式创建主题时,我还在针对该特定主题的单独进程中开始(订阅)新的使用者。

#Consumer code snippet

 if topic_name is not None :
        #Create topic
        create_kafka_topic_instance(topic_name)
        #Initialize a consumer and subscribe to topic
        Process(target=init_kafka_consumer_instance, args=(topic_name))

def forgiving_json_deserializer(v):
    if v is None :
        return
    try:
        return json.loads(v.decode('utf-8'))
    except json.decoder.JSONDecodeError:
        import traceback
        print(traceback.format_exc())
        return None

def init_kafka_consumer_instance(topic, group_id=None):
    try:
        if topic is None:
            raise Exception("Invalid argument topic")
        comsumer = None
        comsumer = KafkaConsumer(topic, bootstrap_servers=[KAFKA_BROKER_URL], auto_offset_reset="earliest",
           urn comsumer
    except Exception as e:
        import traceback
        print(traceback.format_exc())
    return Noneurn comsumer
    except Exception as e:
        import traceback
        print(traceback.format_exc())
    return None

Producer Code Snippet 制片人代码片段

# assuming obj is a model instance
        serialized_obj = serializers.serialize('json', [ order, ])
        #send_message(topic_name,order)
        producer = KafkaProducer(bootstrap_servers=[KAFKA_BROKER_URL], value_serializer=lambda x: json.dumps(x).encode('utf-8'))
        x = producer.send("test", serialized_obj)
        producer.flush()

Now i have some queries, so if somehow my Django application (server) is restarted, will I still have the consumer listening to that topic. 现在我有一些查询,所以如果我的Django应用程序(服务器)以某种方式重新启动,我仍然会让消费者听取该主题。

Also I have some print statements in the consumer which I am unable to see in my server console. 我在消费者中也有一些打印语句,我无法在服务器控制台中看到。

However writing the same code snippet(initialising a consumer) in a python shell, I can see the messages in print statements there, meaning my Producer is working fine. 但是在python shell中编写相同的代码片段(初始化一个使用者),我可以在那里看到print语句中的消息,这意味着我的Producer工作正常。

Kafka Server is not depending on your Django application (server). Kafka Server不依赖于您的Django应用程序(服务器)。 But your Consumer is yes. 但你的消费者是肯定的。

So your topic still alive in Kafka server (if kafka server die, it 's the other story), but your Consumer is restarted with your Application. 所以你的主题在Kafka服务器中仍然存在(如果kafka服务器死了,那就是另一个故事),但你的Consumer会重新启动你的应用程序。

So if you want your consumer work well, make it a Worker that work parallel with your app and will not be restarted when your application is down 因此,如果您希望您的消费者能够正常工作,请将其设置为与您的应用程序并行工作的Worker,并且在您的应用程序关闭时不会重新启动

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

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