forked from celery/kombu
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhello_consumer.py
More file actions
59 lines (49 loc) · 1.7 KB
/
hello_consumer.py
File metadata and controls
59 lines (49 loc) · 1.7 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'''
Importing compatibility features from future Python versions.
absolute_import ensures imports behave like Python 3.
unicode_literals makes all string literals unicode by default.
print_function allows the use of print() as a function.
'''
from __future__ import absolute_import, unicode_literals, print_function
'''
Importing Connection class from kombu library.
Kombu is a messaging library used to interact with message brokers
like RabbitMQ using AMQP protocol.
'''
from kombu import Connection # noqa
'''
Establishing a connection to the message broker.
Connection URL breakdown:
amqp:// -> protocol used (AMQP)
guest:guest -> username and password
localhost -> server where RabbitMQ is running
5672 -> default RabbitMQ port
// -> default virtual host
'''
with Connection('amqp://guest:guest@localhost:5672//') as conn:
'''
Creating or accessing a queue named "simple_queue".
SimpleQueue provides an easy interface for sending
and receiving messages from the queue.
'''
simple_queue = conn.SimpleQueue('simple_queue')
'''
Retrieving a message from the queue.
block=True -> wait until a message arrives
timeout=1 -> wait for only 1 second before raising an error
'''
message = simple_queue.get(block=True, timeout=1)
'''
Printing the message payload (actual data sent in the message).
'''
print('Received: {0}'.format(message.payload))
'''
Acknowledging the message.
This tells the message broker that the message has been
successfully processed and can be removed from the queue.
'''
message.ack()
'''
Closing the queue connection after processing the message.
'''
simple_queue.close()