Pulling out channel setup into common bits

as was suggested in book.
This commit is contained in:
Dan Buch 2012-11-14 18:03:45 -05:00
parent 1120412e45
commit 26ec865dd7
4 changed files with 22 additions and 20 deletions

View File

@ -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

View File

@ -4,6 +4,8 @@ import sys
import pika import pika
from sylvilagus.chapter02 import hello_world
def msg_consumer(channel, method, header, body): def msg_consumer(channel, method, header, body):
channel.basic_ack(delivery_tag=method.delivery_tag) channel.basic_ack(delivery_tag=method.delivery_tag)
@ -16,16 +18,7 @@ def msg_consumer(channel, method, header, body):
def main(): def main():
credentials = pika.PlainCredentials('guest', 'guest') channel = hello_world.get_channel()
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.queue_declare(queue='hello-queue') channel.queue_declare(queue='hello-queue')
channel.queue_bind(queue='hello-queue', channel.queue_bind(queue='hello-queue',

View File

@ -4,18 +4,11 @@ import sys
import pika import pika
from sylvilagus.chapter02 import hello_world
def main(args=sys.argv[:]): def main(args=sys.argv[:]):
credentials = pika.PlainCredentials('guest', 'guest') channel = hello_world.get_channel()
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)
msg = args[1] msg = args[1]
msg_props = pika.BasicProperties() msg_props = pika.BasicProperties()