import asyncio
import confluent_kafka
假设我们想要限制并发任务数量为5
semaphore = asyncio.Semaphore(5)
async def process_message_in_thread(msg):
async with semaphore:
# 在这里处理消息,这将是并发执行的,但受到信号量的限制
print(f"Processing message: {msg.value().decode(‘utf-8’)}")
await asyncio.sleep(1) # 假设处理需要一些时间
async def consume_messages():
# Kafka配置和消费者创建…
try:
while True:
msg = c.poll(1.0)
if msg is None:
continue
if msg.error():
# 处理错误...
continue
# 使用asyncio.to_thread异步提交消息处理到线程池,并受信号量控制
await asyncio.to_thread(process_message_in_thread, msg)
finally:
# 清理...
c.close()
创建并运行消费者任务
asyncio.run(consume_messages())