title: how to use multithreading with pika and rabbitmq to perform Requests and Responses RPC Messages
body: <p>I'm working on a Project with Rabbitmq, I'm using the RPC Pattern, basically I'm receiving or consuming Messages from a Queue, make some Processing and then send a Response back. Im using Pika, my goal is to use a Thread per Task so for every Task i ll make a Thread perticularly for that Task. I also read that the best Practice is to make only one Connection and under it many channels as i want to, but i get always this Error :<br />
  'start_consuming may not be called from the scope of '
  pika.exceptions.RecursionError: start_consuming may not be called from the scope of another BlockingConnection or BlockingChannel callback.</p>
  <p>I made some Research and found out that Pika is not thread safe and we should use for every Thread an Independant Connection and a channel. but i dont want to do that since it is considered bad Practice. So I wanted to ask here if someone already achieved to make this work. I read also that it is Possible if i didn't use BlockingConnection to instantiate my Connection and also that there is a Function called add_callback_threadsafe which can make this Possible. but there is unfortunally no Examples for that and I read the Documentation but it's complex and without Examples it was hard for me to grasp what they want to describe.</p>
  <p>my Try was to declare two Classes. Each class will represent a Task Executer which receive or consume a message from a queue and based on that made some Processing and deliver a Response back. my idea was to share a rabbitmq Connection between the two Tasks but every Task will get an independant Channel. in the Code above the rabbit Parameter passed to the function is a Class that holds some Variables like Connection and other Functions like EventSubscriber which when called it will assign a new Channel and start consuming messages from that Particular Exchanges and routingKey. Next i declare a Thread and give the subscribe or Consume function as a Target to that Thread. the other Task Class look also the same as this Class that's why i ll only upload this Code. in the main Class i make a Connection to rabbitmq and pass it as Parameter to the constructor of the Two Task Classes.</p>
  <p>class On_Deregistration:</p>
  <pre><code>def __init__(self, rabbit):
     self.event(rabbit) # this will call event function and pass the connection shared between all Tasks. rabbit parameter hold a connection to rabbitmq
  
  def event(self, rabbit):
   
      self.Subscriber = rabbit.EventSubscriber(rabbit,  'testing.test',  'test', False,  onDeregistrationFromHRS # this func is task listener)
  
  def subscribeAsync(self):
      self.Subscriber.subscribe() # here i call start_consuming
  
  def start(self):
      &quot;&quot;&quot;start Subscribtion in an Independant Thread  &quot;&quot;&quot;
      thread = threading.Thread(target = self.subscribeAsync )  
      thread.start()
      if thread.isAlive():
          print(&quot;asynchronous subscription started&quot;)
  </code></pre>
  <hr />
  <h1>MAin Class:</h1>
  <p>class App:</p>
  <pre><code>def __init__(self):
  
      self.rabbitMq = RabbitMqCommunicationInterface(host='localhost', port=5672)
      firstTask =  On_Deregistration(self.rabbitMq)
      secondTask =  secondTask(self.rabbitMq)
  </code></pre>
  <p>app = App()</p>
  <h1>error : 'start_consuming may not be called from the scope of '</h1>
  <p>pika.exceptions.RecursionError: start_consuming may not be called from the scope of another BlockingConnection or BlockingChannel callback</p>
  <p>I searched for the cause of this Error and obviously is pika not thread safe but there must be a Solution for this. maybe Not using a BlockingConnection ? maybe someone can give me an Example how to do that because i tried it and didnt work. Maybe I'm missing something about how to Implement multithreading with rabbitmq</p>