From 26ec865dd7473673c6c87cd386ea28aa37ae3391 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 14 Nov 2012 18:03:45 -0500 Subject: [PATCH] Pulling out channel setup into common bits as was suggested in book. --- .../python/sylvilagus/chapter02/__init__.py | 0 .../python/sylvilagus/chapter02/hello_world.py | 16 ++++++++++++++++ .../sylvilagus/chapter02/hello_world_consumer.py | 13 +++---------- .../sylvilagus/chapter02/hello_world_producer.py | 13 +++---------- 4 files changed, 22 insertions(+), 20 deletions(-) create mode 100644 sylvilagus/python/sylvilagus/chapter02/__init__.py create mode 100644 sylvilagus/python/sylvilagus/chapter02/hello_world.py diff --git a/sylvilagus/python/sylvilagus/chapter02/__init__.py b/sylvilagus/python/sylvilagus/chapter02/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sylvilagus/python/sylvilagus/chapter02/hello_world.py b/sylvilagus/python/sylvilagus/chapter02/hello_world.py new file mode 100644 index 0000000..879fb9e --- /dev/null +++ b/sylvilagus/python/sylvilagus/chapter02/hello_world.py @@ -0,0 +1,16 @@ +import pika + + +def get_channel(): + credentials = pika.PlainCredentials('guest', 'guest') + conn_params = pika.ConnectionParameters('localhost', + credentials=credentials) + channel = pika.BlockingConnection(conn_params).channel() + channel.exchange_declare(exchange='hello-exchange', + type='direct', + passive=False, + durable=True, + auto_delete=False) + + return channel + diff --git a/sylvilagus/python/sylvilagus/chapter02/hello_world_consumer.py b/sylvilagus/python/sylvilagus/chapter02/hello_world_consumer.py index 395752c..e1802e4 100644 --- a/sylvilagus/python/sylvilagus/chapter02/hello_world_consumer.py +++ b/sylvilagus/python/sylvilagus/chapter02/hello_world_consumer.py @@ -4,6 +4,8 @@ import sys import pika +from sylvilagus.chapter02 import hello_world + def msg_consumer(channel, method, header, body): channel.basic_ack(delivery_tag=method.delivery_tag) @@ -16,16 +18,7 @@ def msg_consumer(channel, method, header, body): def main(): - credentials = pika.PlainCredentials('guest', 'guest') - conn_params = pika.ConnectionParameters('localhost', - credentials=credentials) - conn_broker = pika.BlockingConnection(conn_params) - channel = conn_broker.channel() - channel.exchange_declare(exchange='hello-exchange', - type='direct', - passive=False, - durable=True, - auto_delete=False) + channel = hello_world.get_channel() channel.queue_declare(queue='hello-queue') channel.queue_bind(queue='hello-queue', diff --git a/sylvilagus/python/sylvilagus/chapter02/hello_world_producer.py b/sylvilagus/python/sylvilagus/chapter02/hello_world_producer.py index 4ee5094..96e78a5 100644 --- a/sylvilagus/python/sylvilagus/chapter02/hello_world_producer.py +++ b/sylvilagus/python/sylvilagus/chapter02/hello_world_producer.py @@ -4,18 +4,11 @@ import sys import pika +from sylvilagus.chapter02 import hello_world + def main(args=sys.argv[:]): - credentials = pika.PlainCredentials('guest', 'guest') - conn_params = pika.ConnectionParameters('localhost', - credentials=credentials) - conn_broker = pika.BlockingConnection(conn_params) - channel = conn_broker.channel() - channel.exchange_declare(exchange='hello-exchange', - type='direct', - passive=False, - durable=True, - auto_delete=False) + channel = hello_world.get_channel() msg = args[1] msg_props = pika.BasicProperties()