Search code examples
multithreadingpython-2.7rabbitmqpython-multiprocessingpika

multiprocess rabbit consumer


Thanks in advance. I'm new in Multiprocessing. I've created a process through which i want to consume the data trough Rabbit MQueue at same time but it runs one process at a time.

def start_consum(queue_name):
    channel.basic_consume(func, queue=queue_name)
    channel.start_consuming()

def process_start(number):
    from multiprocessing import Process
    events = ["ev1","ev2","ev3"]
    for process in range(number):
        for event in events:
            proc = Process(target= start_consum(event))
            proc.daemon = True
            proc.start()


process_start(10)

In above code it start consuming the first event and then it starts the 2nd one.


Solution

  • You can start a thread for this.

    class Threaded_worker(threading.Thread):
        def __init__(self):
            threading.Thread.__init__(self)
            self.credentials = pika.PlainCredentials('', '')
            self.connection = pika.BlockingConnection(pika.ConnectionParameters(credentials=self.credentials,host=))
            self.channel = self.connection.channel()
            self.channel.basic_qos(prefetch_count=1)
            events = ["ev1","ev2","ev3"]
            for event in events:
                self.channel.basic_consume(func, queue=event)             
    
        def run(self):
            print 'start consuming'
            self.channel.start_consuming()
    
        def thread_start(numberofthreads):                
            for _ in range(numberofthreads):
                td = Threaded_worker()
                td.setDaemon(True)
                td.start()