title: Make rabbitMQ connections accessible to other modules
body: <p>RabbitMQ best practices suggest using long-lived connections, ideally separate Consume and Publish connections, and attach a channel per thread to the corresponding connection. I am building a distributed system where every part needs to consume and publish messages to other parts of the system. Class RabbitMQ creates those connections, attaches channels to them, and publishes messages. On the other hand I have around 10 processes, each in a thread, that must consume/publish through its "own" channel. On startup each processes creates its channel and binds its queues.</p>
  
  <p>My question is how to start a <em>unique</em> instance of class RabbitMQ that makes the two connections "accessible" to the processes, keeping those two connections alive and avoiding opnening/closing channels. I tried <strong>import messaging</strong> in each module, but for every import there is an instantiation of the class and, therefore two new connections. I also tried adding a singleton to class RabbitMQ to avoid multiple instantiations on imports but did not work.</p>
  
  <p>I appreciate your help.</p>
  
  <h1>messaging.py</h1>
  
  <pre><code>class RabbitMQ:
  
      def __init__(self):
          self.consume_connection = None
          self.publish_connection = None
          self.initialize_connection()
  
      def initialize_connection(self):
          self.consume_connection = pika.BlockingConnection(pika.ConnectionParameters(
              host='localhost', socket_timeout=5, client_properties={'connection_name': 'consume_connection'}))
          self.publish_connection = pika.BlockingConnection(pika.ConnectionParameters(
              host='localhost', socket_timeout=5, client_properties={'connection_name': 'publish_connection'}))
  
      def send_message(self, exchange_name, routing_key, message, channel):
          ...
  
      def create_consume_channel(self):
          ...
  
      def create_publish_channel(self):
          ...
  
  
  Messaging = RabbitMQ()
  </code></pre>
  
  <h1>consuming_process.py</h1>
  
  <pre><code>...
  def connect_messaging(self):
      channel = self.messaging.create_consume_channel() # &lt;-- messaging would be the instance of class RabbitMQ
      channel.basic_qos(prefetch_count=100)
  
      exchange_name = 'abc'
      channel.exchange_declare(exchange=exchange_name, exchange_type='direct')
  
      result = channel.queue_declare(queue='queue_name')
      queue_1 = result.method.queue
      channel.queue_bind(exchange=exchange_name, queue=queue_1, routing_key='some_routing_key')
      ...    
  
      def callback_function(ch, method, properties, body):
          ...
          ch.basic_ack(delivery_tag=method.delivery_tag)
  
      channel.basic_consume(callback_function, queue=queue_1, no_ack=False)
      channel.start_consuming()
  </code></pre>