Is it ok to use Pika BlockingConnection in web app?
Asked Answered
T

1

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.

Tyre answered 30/7, 2014 at 22:29 Comment(0)
W
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.

Willumsen answered 30/7, 2014 at 22:54 Comment(2)
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?Tyre
@Tyre 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.Willumsen

© 2022 - 2024 — McMap. All rights reserved.