Python 向主题重新发送消息的正确方法

Python 向主题重新发送消息的正确方法,python,apache-kafka,faust,Python,Apache Kafka,Faust,我将消息从卡夫卡主题加载到数据库。加载到数据库可能会失败。我也不想丢失未发送的消息 应用程序代码: import faust app = faust.App('App', broker='kafka://localhost:9092') source_topic = app.topic('source_topic') failed_channel = app.channel() # channel for unsent messages @app.agent(source_topic)

我将消息从卡夫卡主题加载到数据库。加载到数据库可能会失败。我也不想丢失未发送的消息

应用程序代码:

import faust

app = faust.App('App', broker='kafka://localhost:9092')

source_topic = app.topic('source_topic')
failed_channel = app.channel()  # channel for unsent messages


@app.agent(source_topic)
async def process(stream):
    async for batch in stream.take(100_000, within=60):
        # here we have not info about partitions and keys
        # to reuse them when resending if sending failed
        try:
            pass  # send to database.  can fail
        except ConnectionError:
            for record in batch:
                # sending to channel is faster than sending to topic
                await failed_channel.send(value=record)


@app.agent(failed_channel)
async def resend_failed(stream):
    async for unsent_msg in stream:
        await source_topic.send(value=unsent_msg)
也许有更标准的方法来处理这种情况?添加app.topic('source\u topic',acks=False)仅在重新启动app后有效

我将消息从卡夫卡主题加载到数据库

也许有更标准的方法来处理这种情况

是的-它叫卡夫卡连接:-)

标准模式是对数据进行任何处理并将其写入[返回]卡夫卡主题。然后使用Kafka主题作为Kafka Connect接收器连接器的源,在本例中为

Kafka Connect是Apache Kafka的一部分,处理重启、扩展、故障等

另见