Kaninchen & Schlangen: RabbitMQ & Python
Kaninchen & Schlangen: RabbitMQ & Python
Quelle: http://www.redhat.com/docs/en-US/Red_Hat_Enterprise_MRG/1.0/html/Messaging_Tutorial/index.html
Fanout Exchange
Quelle: http://www.redhat.com/docs/en-US/Red_Hat_Enterprise_MRG/1.0/html/Messaging_Tutorial/index.html
Direct Exchange
Quelle: http://www.redhat.com/docs/en-US/Red_Hat_Enterprise_MRG/1.0/html/Messaging_Tutorial/index.html
Topic Exchange
Quelle: http://www.redhat.com/docs/en-US/Red_Hat_Enterprise_MRG/1.0/html/Messaging_Tutorial/index.html
RabbitMQ
• AMQP Message Broker
• Erlang
• Open Source
• Mitglied in der AMQP Working Group
• XMPP, SMTP, STOMP und HTTP (mit
Adaptern)
• http://www.rabbitmq.com/
Virtual Host Access Control
mit RabbitMQ
Konfiguration
Schreiben
Lesen
Python Bibliotheken
connection.py
from carrot.connection import BrokerConnection
class MessageConsumer(Consumer):
def receive(self, message_data, message):
data = (message.delivery_tag, message_data['message'],
message_data['message_id'],
message_data['timestamp'].isoformat())
print 'Message %d "%s" with id %s sent at %s.' % data
message.ack()
if __name__ == '__main__':
consumer = MessageConsumer(connection=conn, queue=queue,
exchange=exchange, routing_key=routing_key)
consumer.wait()
Celery
Distributed Task Queue
• Backends: RabbitMQ, STOMP, Redis, Ghetto
Queue
• Clustering mit RabbitMQ
• Webhooks
• Django-Integration (optional)
• Autor: Ask Solem
• http://github.com/ask/celery/
Celery
Distributed Task Queue
class CreateUserTask(Task):
def run(self, username, password):
User.create(username, password)
@task()
def create_user(username, password):
User.create(username, password)
CELERY_RESULT_BACKEND = "database"
CELERY_RESULT_DBURI = "mysql://user:password@host/dbname"
CELERY_IMPORTS = ("tasks", )
$ celeryd --loglevel=INFO
Zeitgesteuerter Task
# Periodic task
from celery.decorators import periodic_task
from datetime import timedelta
@periodic_task(run_every=timedelta(seconds=30))
def every_30_seconds():
print("Running periodic task!")
# crontab
from celery.task.schedules import crontab
from celery.decorators import periodic_task
$ celerybeat
$ celeryd -B
Fotos der Titelfolie