from functools import wraps import pika from .settings import RABBITMQ_HOST def with_pika_channel(f): @wraps(f) def wrapper(*args, **kwargs): # if we are already passing channel, just reuse it if 'channel' in kwargs: return f(*args, **kwargs) # otherwise set up a new connection and channel to wrap our code with connection = pika.BlockingConnection( pika.ConnectionParameters(host=RABBITMQ_HOST)) channel = connection.channel() ret = f(*args, **kwargs, channel=channel) channel.close() return ret return wrapper