diff options
author | lowercase00 <21188280+lowercase00@users.noreply.github.com> | 2023-03-07 00:54:47 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-07 10:54:47 +0700 |
commit | d5bde117c2f5477b5d0ba9846f2a7c4480ca1854 (patch) | |
tree | 7e43df189b474787860ade15c0f865055b8a0a85 /docs | |
parent | 7f9f0f72ba1adac907791d604d5f56af8125fdfd (diff) | |
download | rq-d5bde117c2f5477b5d0ba9846f2a7c4480ca1854.tar.gz |
Remove `use_connection` (#1859)
* feat: remove use_connection
* fix: clear old test
Diffstat (limited to 'docs')
-rw-r--r-- | docs/docs/connections.md | 67 |
1 files changed, 17 insertions, 50 deletions
diff --git a/docs/docs/connections.md b/docs/docs/connections.md index f58da10..4382cef 100644 --- a/docs/docs/connections.md +++ b/docs/docs/connections.md @@ -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 |