|
|
|
@ -3,61 +3,20 @@ title: "RQ: Connections"
|
|
|
|
|
layout: docs
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
Although RQ features the `use_connection()` command for convenience, it
|
|
|
|
|
is deprecated, since it pollutes the global namespace. Instead, prefer explicit
|
|
|
|
|
connection management using the `with Connection(...):` context manager, or
|
|
|
|
|
pass in Redis connection references to queues directly.
|
|
|
|
|
### The connection parameter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Single Redis connection (easy)
|
|
|
|
|
|
|
|
|
|
<div class="warning">
|
|
|
|
|
<img style="float: right; margin-right: -60px; margin-top: -38px" src="/img/warning.png" />
|
|
|
|
|
<strong>Note:</strong>
|
|
|
|
|
<p>
|
|
|
|
|
The use of <code>use_connection</code> is deprecated.
|
|
|
|
|
Please don't use <code>use_connection</code> in your scripts.
|
|
|
|
|
Instead, use explicit connection management.
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
In development mode, to connect to a default, local Redis server:
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
from rq import use_connection
|
|
|
|
|
use_connection()
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
In production, to connect to a specific Redis server:
|
|
|
|
|
Each RQ object (queues, workers, jobs) has a `connection` keyword
|
|
|
|
|
argument that can be passed to the constructor - this is the recommended way of handling connections.
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
from redis import Redis
|
|
|
|
|
from rq import use_connection
|
|
|
|
|
from rq import Queue
|
|
|
|
|
|
|
|
|
|
redis = Redis('my.host.org', 6789, password='secret')
|
|
|
|
|
use_connection(redis)
|
|
|
|
|
redis = Redis('localhost', 6789)
|
|
|
|
|
q = Queue(connection=redis)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Be aware of the fact that `use_connection` pollutes the global namespace. It
|
|
|
|
|
also implies that you can only ever use a single connection.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Multiple Redis connections
|
|
|
|
|
|
|
|
|
|
However, the single connection pattern facilitates only those cases where you
|
|
|
|
|
connect to a single Redis instance, and where you affect global context (by
|
|
|
|
|
replacing the existing connection with the `use_connection()` call). You can
|
|
|
|
|
only use this pattern when you are in full control of your web stack.
|
|
|
|
|
|
|
|
|
|
In any other situation, or when you want to use multiple connections, you
|
|
|
|
|
should use `Connection` contexts or pass connections around explicitly.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Explicit connections (precise, but tedious)
|
|
|
|
|
|
|
|
|
|
Each RQ object instance (queues, workers, jobs) has a `connection` keyword
|
|
|
|
|
argument that can be passed to the constructor. Using this, you don't need to
|
|
|
|
|
use `use_connection()`. Instead, you can create your queues like this:
|
|
|
|
|
This pattern allows for different connections to be passed to different objects:
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
from rq import Queue
|
|
|
|
@ -73,11 +32,19 @@ q2 = Queue('bar', connection=conn2)
|
|
|
|
|
Every job that is enqueued on a queue will know what connection it belongs to.
|
|
|
|
|
The same goes for the workers.
|
|
|
|
|
|
|
|
|
|
This approach is very precise, but rather verbose, and therefore, tedious.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Connection contexts (precise and concise)
|
|
|
|
|
|
|
|
|
|
<div class="warning">
|
|
|
|
|
<img style="float: right; margin-right: -60px; margin-top: -38px" src="/img/warning.png" />
|
|
|
|
|
<strong>Note:</strong>
|
|
|
|
|
<p>
|
|
|
|
|
The use of <code>Connection</code> context manager is deprecated.
|
|
|
|
|
Please don't use <code>Connection</code> in your scripts.
|
|
|
|
|
Instead, use explicit connection management.
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
There is a better approach if you want to use multiple connections, though.
|
|
|
|
|
Each RQ object instance, upon creation, will use the topmost Redis connection
|
|
|
|
|
on the RQ connection stack, which is a mechanism to temporarily replace the
|
|
|
|
|