Hello World fent servir la llibreria 'kombu' i python
Traducció de l’original en anglès. Llegeix-lo en: CastellanoEnglish
Part 4 de 4AMQP i RabbitMQ
- AMQP i RabbitMQ [TOC]
- Què és AMQP? i l'arquitectura
- A fons dins d'AMQP
- Hello World fent servir la llibreria 'kombu' i python
A vegades els esquemes i els fragments de codi no necessiten grans descripcions. Si creus que en aquest cas no n'hi ha prou, digues-m'ho i afegiré explicacions.
Fent servir una llibreria de python anomenada kombu com a abstracció per parlar amb el broker AMQP, desenvoluparem diferents rutes de missatges configurant cada tipus d'Exchange. Com a backend vaig fer servir RabbitMQ amb la configuració per defecte.
Esquema AMQP fent servir un exchange de tipus direct
Definició de la cua:
from kombu import Exchange, Queue
task_exchange = Exchange("msgs", type="direct")
queue_msg_1 = Queue("messages_1", task_exchange, routing_key = 'message_1')
queue_msg_2 = Queue("messages_2", task_exchange, routing_key = 'message_2')
El productor:
from __future__ import with_statement
from queues import task_exchange
from kombu.common import maybe_declare
from kombu.pools import producers
if __name__ == "__main__":
from kombu import BrokerConnection
connection = BrokerConnection("amqp://guest:guest@localhost:5672//")
with producers[connection].acquire(block=True) as producer:
maybe_declare(task_exchange, producer.channel)
payload = {"type": "handshake", "content": "hello #1"}
producer.publish(payload, exchange = 'msgs', serializer="pickle", routing_key = 'message_1')
payload = {"type": "handshake", "content": "hello #2"}
producer.publish(payload, exchange = 'msgs', serializer="pickle", routing_key = 'message_2')
Un consumidor:
from queues import queue_msg_1
from kombu.mixins import ConsumerMixin
class C(ConsumerMixin):
def __init__(self, connection):
self.connection = connection
return
def get_consumers(self, Consumer, channel):
return [Consumer( queue_msg_1, callbacks = [ self.on_message ])]
def on_message(self, body, message):
print ("RECEIVED MSG - body: %r" % (body,))
print ("RECEIVED MSG - message: %r" % (message,))
message.ack()
return
if __name__ == "__main__":
from kombu import BrokerConnection
from kombu.utils.debug import setup_logging
setup_logging(loglevel="DEBUG")
with BrokerConnection("amqp://guest:guest@localhost:5672//") as connection:
try:
C(connection).run()
except KeyboardInterrupt:
print("bye bye")
Esquema AMQP fent servir un exchange de tipus fanout
Definició de la cua:
from kombu import Exchange, Queue
task_exchange = Exchange("ce", type="fanout")
queue_events_db = Queue("events.db", task_exchange)
queue_events_notify = Queue("events.notify", task_exchange)
El productor:
from __future__ import with_statement
from queues import task_exchange
from kombu.common import maybe_declare
from kombu.pools import producers
if __name__ == "__main__":
from kombu import BrokerConnection
connection = BrokerConnection("amqp://guest:guest@localhost:5672//")
with producers[connection].acquire(block=True) as producer:
maybe_declare(task_exchange, producer.channel)
payload = {"operation": "create", "content": "the object"}
producer.publish(payload, exchange = 'ce', serializer="pickle", routing_key = 'user.write')
payload = {"operation": "update", "content": "updated fields", "id": "id of the object"}
producer.publish(payload, exchange = 'ce', serializer="pickle", routing_key = 'user.write')
Un consumidor:
from queues import queue_events_db
from kombu.mixins import ConsumerMixin
class C(ConsumerMixin):
def __init__(self, connection):
self.connection = connection
return
def get_consumers(self, Consumer, channel):
return [Consumer( queue_events_db, callbacks = [self.on_message])]
def on_message(self, body, message):
print ("save_db: RECEIVED MSG - body: %r" % (body,))
print ("save_db: RECEIVED MSG - message: %r" % (message,))
message.ack()
return
if __name__ == "__main__":
from kombu import BrokerConnection
from kombu.utils.debug import setup_logging
setup_logging(loglevel="DEBUG")
with BrokerConnection("amqp://guest:guest@localhost:5672//") as connection:
try:
C(connection).run()
except KeyboardInterrupt:
print("bye bye")
Esquema AMQP fent servir un exchange de tipus topic
Definició de la cua:
from kombu import Exchange, Queue
task_exchange = Exchange("user", type="topic")
queue_user_write = Queue("user.write", task_exchange, routing_key = 'user.write')
queue_user_read = Queue("user.read", task_exchange, routing_key = 'user.read')
queue_notify = Queue("notify", task_exchange, routing_key = 'user.#')
El productor:
from __future__ import with_statement
from queues import task_exchange
from kombu.common import maybe_declare
from kombu.pools import producers
if __name__ == "__main__":
from kombu import BrokerConnection
connection = BrokerConnection("amqp://guest:guest@localhost:5672//")
with producers[connection].acquire(block=True) as producer:
maybe_declare(task_exchange, producer.channel)
payload = {"operation": "create", "content": "the object"}
producer.publish(payload, exchange = 'user', serializer="pickle", routing_key = 'user.write')
payload = {"operation": "update", "content": "updated fields", "id": "id of the object"}
producer.publish(payload, exchange = 'user', serializer="pickle", routing_key = 'user.write')
payload = {"operation": "delete", "id": "id of the object"}
producer.publish(payload, exchange = 'user', serializer="pickle", routing_key = 'user.write')
payload = {"operation": "read", "id": "id of the object"}
producer.publish(payload, exchange = 'user', serializer="pickle", routing_key = 'user.read')
Un consumidor:
from queues import queue_events_db
from kombu.mixins import ConsumerMixin
class C(ConsumerMixin):
def __init__(self, connection):
self.connection = connection
return
def get_consumers(self, Consumer, channel):
return [Consumer( queue_events_db, callbacks = [self.on_message])]
def on_message(self, body, message):
print ("save_db: RECEIVED MSG - body: %r" % (body,))
print ("save_db: RECEIVED MSG - message: %r" % (message,))
message.ack()
return
if __name__ == "__main__":
from kombu import BrokerConnection
from kombu.utils.debug import setup_logging
setup_logging(loglevel="DEBUG")
with BrokerConnection("amqp://guest:guest@localhost:5672//") as connection:
try:
C(connection).run()
except KeyboardInterrupt:
print("bye bye")
Cues simples
Kombu implementa SimpleQueue i SimpleBuffer com a solució senzilla per a cues amb exchange de tipus 'direct', amb el mateix nom d'exchange, routing key i nom de cua.
Qui publica:
from kombu import BrokerConnection
connection = BrokerConnection("amqp://guest:guest@localhost:5672//")
queue = connection.SimpleQueue("logs")
payload = { "severity":"info", "message":"this is just a log", "ts":"2013/09/30T15:10:23" }
queue.put(payload, serializer='pickle')
queue.close()
Qui consumeix:
from kombu import BrokerConnection
from Queue import Empty
connection = BrokerConnection("amqp://guest:guest@localhost:5672//")
queue = connection.SimpleQueue("logs")
while 1:
try:
message = queue.get(block=True, timeout=1)
print message.payload
message.ack()
except Empty:
pass
except KeyboardInterrupt:
break
print message
queue.close()
Els fitxers
Descarrega tots els fitxers d'exemple: kombu-tests.tar.gz
5 comentaris
Els comentaris del blog antic, tal com es van escriure. Els comentaris estan tancats.



Richard Loo
thanks for sharing, great post.
I have one question, what is "maybe_declare" used for?
Oriol Rius
It's used to create, if it's necessary, the entities of RabbitMQ: exchange, binding and queues. If the entities was created before it does nothing.
Germán Larraín
It's like "create if it does not exist"
Anuj
I know this question does not make sense..But can I extend this to run on Django Web Server and create an IOS app that use API to send and receive message, more like chat application ?
gamesbook
Rather look to use Django REST API and a tool such SwampDragon.