Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rabbitmq using threads with pika

I'm trying to get a basic queue system with rabbitmq, but when I try to use threads, it only seems to run 1 thread.

my code:

import pika
import threading

rabbit_url = "amqp://user:[email protected]:5672/%2f"

def start(max_threads):
    for i in xrange(max_threads):
        t = threading.Thread(target=run)
        t.start()
        t.join()

def run():
    connection = pika.BlockingConnection(pika.URLParameters(rabbit_url))
    channel = connection.channel()
    channel.basic_consume(callback,
                          queue='docketq',
                          no_ack=True)

    channel.start_consuming()

def callback(ch, method, properties, body):
    do_work(body)

def do_work(body):
    print body
like image 489
leakybytes Avatar asked Dec 05 '25 10:12

leakybytes


2 Answers

t.join() waits for the thread to finish. In the first iteration of the loop in start() you start the first thread and then wait for it to finish, but it never will because channel.start_consuming() is an infinite loop waiting for incoming messages.

like image 70
dubek Avatar answered Dec 08 '25 00:12

dubek


Pika is not threadsafe. From the Pika FAQ:

Is Pika thread safe?

Pika does not have any notion of threading in the code. If you want to use Pika with threading, make sure you have a Pika connection per thread, created in that thread. It is not safe to share one Pika connection across threads.

like image 24
weinv Avatar answered Dec 08 '25 01:12

weinv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!