title: Python - Asynch Multiprocessing from RabbitMQ Consumer
body: <p>I have a Python program that acts as a consumer for RabbitMQ. Once it receives a job from its queue, I want the program to split the job up using multiprocessing, but I'm running into issues with the logistics of multiprocessing.</p>
  
  <p>I've simplified the code for readability.</p>
  
  <p>My RabbitMQ consumer functionality:</p>
  
  <pre><code>connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
  channel = connection.channel()
  
  channel.queue_declare(queue="JobReader", durable=True)
  logging.info('Waiting for messages..')
  
  
  def callback(ch, method, properties, body):
      job_info = json.loads(body)
  
      logging.info('Start Time: ' + time.strftime("%H:%M:%S"))
  
      split_jobs = split_job(job_info)
  
      process_manager.runProcesses(split_jobs)
  
      ch.basic_ack(delivery_tag=method.delivery_tag)
  </code></pre>
  
  <p>My multiprocessing functionality:</p>
  
  <pre><code>#!/usr/bin/python
  
  import multiprocessing
  import other_package
  
  
  def worker_process(sub_job):
      other_package.run_job(sub_job)
  
  
  def runProcesses(jobs):
      processes = []
      for sub_job in jobs:
          p = multiprocessing.Process(target=worker_process, args=(sub_job,))
          processes.append(p)
  
          p.start()
  </code></pre>
  
  <p>Naturally, I can't do <code>if __name__ == '__main__':</code> because it is within a function.</p>
  
  <p>I'm not sure if there is a workaround for this with multiprocessing, or if I'm just approaching this the wrong way. Any help would be greatly appreciated.</p>