Sha*_*ulu 1 python async-await apache-kafka python-asyncio
我正在尝试实现 Python aiokafka 异步库,但由于某种原因我无法异步处理消息。
我创建了异步消费者、生产者并使用了 asyncio python 库。
python 3.7.2
aiokafka==0.5.1
kafka-python==1.4.3
Run Code Online (Sandbox Code Playgroud)
from aiokafka import AIOKafkaConsumer
import asyncio
import json
import ast
loop = asyncio.get_event_loop()
async def consume():
consumer = AIOKafkaConsumer(
"test_topic", loop=loop, bootstrap_servers='localhost:9092')
# Get cluster layout and topic/partition allocation
await consumer.start()
try:
async for msg in consumer:
sleep_time = ast.literal_eval(json.loads(msg.value))
print('before sleep %s' % sleep_time)
await asyncio.sleep(sleep_time)
print('after sleep %s' % sleep_time)
finally:
await consumer.stop()
loop.run_until_complete(consume())
Run Code Online (Sandbox Code Playgroud)
import json
import uuid
from kafka import KafkaProducer, KafkaConsumer
class KafkaClient(object):
def __init__(self, topic_name=None, consume=True):
"""
Initial consumer and producer for Kafka
:param topic_name: consumer topic name
"""
self.topic_name = topic_name
if topic_name is not None:
self.kafka_connect(topic_name, source='SOURCE')
self.producer = KafkaProducer(bootstrap_servers='localhost:9092',
key_serializer=str.encode,
value_serializer=lambda m: json.dumps(m).encode('utf-8'))
def publish_message(self, topic_name, message, extra_data=None):
try:
msg_uid = str(uuid.uuid1())
self.producer.send(topic_name, value=json.dumps(message))
self.producer.flush()
print('Message published [msg_uid]: %s' % msg_uid)
return True
except Exception as err:
print(err)
return False
k = KafkaClient()
for i in range(0, 1):
k.publish_message('test_topic', 5)
k.publish_message('test_topic', 3)
k.publish_message('test_topic', 1)
Run Code Online (Sandbox Code Playgroud)
该过程将打印:
before sleep 5
before sleep 3
before sleep 1
after sleep 1
after sleep 3
after sleep 5
Run Code Online (Sandbox Code Playgroud)
该过程打印
before sleep 5
after sleep 5
before sleep 3
after sleep 3
before sleep 1
after sleep 1
Run Code Online (Sandbox Code Playgroud)
就其本身而言,async for它不会并行处理序列——它只允许协程在等待异步可迭代对象生成下一个项目时挂起。您可以将其视为特殊方法await上的一系列s __anext__,类似于普通方法是对 的一系列调用__next__。
但是很容易产生在消息到达时处理消息的任务。例如:
async def process(msg):
sleep_time = ast.literal_eval(json.loads(msg.value))
print('before sleep %s' % sleep_time)
await asyncio.sleep(sleep_time)
print('after sleep %s' % sleep_time)
async def consume():
consumer = AIOKafkaConsumer(
"test_topic", loop=loop, bootstrap_servers='localhost:9092')
await consumer.start()
tasks = []
try:
async for msg in consumer:
tasks.append(asyncio.create_task(process(msg))
finally:
await consumer.stop()
await asyncio.gather(*tasks)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1635 次 |
| 最近记录: |