X
X
Xcopy2014-12-15 02:37:13
Django
Xcopy, 2014-12-15 02:37:13

Where in Django do you declare global variables like connection etc.?

Hello!
I have a Django server and for certain requests I need to send jobs to another RabbitMQ server.
I do this using the pika AMQP library (as here )
In order to send a request to RabbitMQ using pika, you need:

# создать подключение
connection = pika.BlockingConnection(pika.ConnectionParameters(host='my_IP_addres'))
# канал
channel = connection.channel()
# объявить очередь
channel.queue_declare(queue='gcm_sender')
# и послать в нее все, что душе угодно
channel.basic_publish(exchange='', routing_key='gcm_sender', body='все, что душе угодно')

Naturally, creating a connection, a channel and declaring a queue in each request (in a specific views.py handler) is not correct - because this will happen on every request.
Obviously, I need to create a connection, a channel and a queue once, and use only send in views
channel.basic_publish(exchange='', routing_key='gcm_sender', body='все, что душе угодно')

The only thing is, I don't know where it is correct to declare such global objects... I thought about settings.py... but still, these are not settings... So, I would like to know your professional opinion.
Where do you usually declare global objects?
Thanks in advance for your reply.
PS By the way, the same applies to the object logger = logging.getLogger(__name__)while I declare it in views.py, right after the imports...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Ali Aliyev, 2014-12-15
@ali_aliev

You can't declare global objects, they break the architecture and can cause a lot of problems! For constant variables and settings, it's OK to declare them in settings.py.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question