6

I'm a little bit confused about BlockingConnection and AsyncoreConnection. I want to send some messages to the RabbitMQ queue from a Django app. Is it ok to do that using a global BlockingConnection object?

Thank You.

dano
  • 91,354
  • 19
  • 222
  • 219
User
  • 1,978
  • 5
  • 26
  • 47

1 Answers1

8

You need to have one BlockingConnection object per thread, as stated in the pika FAQ:

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.

So, the answer depends on how you're deploying Django. If you're using Django in a multi-threaded deployment, you can't use a global BlockingConnection; you need to create one per-thread. If you're not using multi-threading, you can use a global BlockingConnection object.

dano
  • 91,354
  • 19
  • 222
  • 219
  • I don't use threads explicite but deploy django using uWSGI and flags: `enable-threads = true`, `process = 8`. Should I create connection every request? Is there a sense using `pika.SelectConnection` in a synchronous application? – User Jul 31 '14 at 06:53
  • 2
    @User I think that with those settings you'll get 8 single-threaded processes, so you should be able use a global `BlockingConnection`. It doesn't make sense to use a `SelectConnection` because you're not using an event loop in your application. Plus, since you're just using `BlockingConnection` to publish messages (rather than to consume them), you shouldn't be blocking on RabbitMQ calls for very long. – dano Jul 31 '14 at 14:10