added rabbitmq helpers and setup command
This commit is contained in:
25
rmq_helper/extension.py
Normal file
25
rmq_helper/extension.py
Normal file
@@ -0,0 +1,25 @@
|
||||
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
|
||||
Reference in New Issue
Block a user