-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbasic_consumer_offset_next.py
44 lines (34 loc) · 1.05 KB
/
basic_consumer_offset_next.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import asyncio
import signal
from rstream import (
AMQPMessage,
Consumer,
ConsumerOffsetSpecification,
MessageContext,
OffsetType,
amqp_decoder,
)
STREAM = "my-test-stream"
async def consume():
consumer = Consumer(
host="localhost",
port=5552,
vhost="/",
username="guest",
password="guest",
)
loop = asyncio.get_event_loop()
loop.add_signal_handler(signal.SIGINT, lambda: asyncio.create_task(consumer.close()))
async def on_message(msg: AMQPMessage, message_context: MessageContext):
stream = message_context.consumer.get_stream(message_context.subscriber_name)
offset = message_context.offset
print("Got message: {}".format(msg) + " from stream " + stream + " offset: " + str(offset))
await consumer.start()
await consumer.subscribe(
stream=STREAM,
callback=on_message,
decoder=amqp_decoder,
offset_specification=ConsumerOffsetSpecification(OffsetType.NEXT),
)
await consumer.run()
asyncio.run(consume())