多进程兔消费者

时间:2017-02-15 11:11:18

标签: multithreading python-2.7 rabbitmq python-multiprocessing pika

提前致谢。 我是Multiprocessing的新手。 我已经创建了一个流程,通过该流程我想同时通过Rabbit MQueue使用数据,但它一次只运行一个流程。

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)

在上面的代码中,它开始消耗第一个事件,然后它开始第二个事件。

1 个答案:

答案 0 :(得分:0)

你可以为此开始一个线程。

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()