3

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:pass!@127.0.0.1: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
leakybytes
  • 359
  • 4
  • 12

2 Answers2

6

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.

dubek
  • 11,447
  • 5
  • 30
  • 23
  • In general, place the t.start() and t.join() in the same loop, will make similar error. – F.Tamy Feb 02 '22 at 10:07
  • How to stop `channel.start_consuming()`? – Gulzar Feb 10 '22 at 09:14
  • You need to import multiprocessing and In a new process use a thread as consumer. If need to stop the start_consuming, you need to terminate the new process from the main process. – F.Tamy Apr 14 '22 at 14:05
  • @F.Tamy can you post example code? I am facing this issue and cant find a solution –  Nov 17 '22 at 12:27
  • @Gulzar can you post example code? I am facing this issue and cant find a solution –  Nov 17 '22 at 12:27
  • @RandString What are you talking about? – Gulzar Nov 17 '22 at 12:36
  • @Gulzar how did you solve this issue? Like how do you use catch the errors thrown by pika inside thread? thanks –  Nov 17 '22 at 14:13
  • @RandString I am not the OP here. Catching errors is done the same in a thread as anywhere else, use try..except – Gulzar Nov 17 '22 at 14:34
5

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.

Community
  • 1
  • 1
weinv
  • 59
  • 1
  • 1
    But no connection is shared, since the connection is created in `run`, which is executed once per thread. – Right leg Nov 20 '17 at 08:56