Skip to content

Commit a49e656

Browse files
authored
RESP3 connection examples (#2863)
1 parent 471f860 commit a49e656

File tree

3 files changed

+203
-84
lines changed

3 files changed

+203
-84
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ The table below higlights version compatibility of the most-recent library versi
4545
| Library version | Supported redis versions |
4646
|-----------------|-------------------|
4747
| 3.5.3 | <= 6.2 Family of releases |
48-
| >= 4.1.0 | Version 5.0 to current |
48+
| >= 4.5.0 | Version 5.0 to 7.0 |
49+
| >= 5.0.0 | Versiond 5.0 to current |
4950

5051

5152
## Usage
@@ -63,6 +64,15 @@ b'bar'
6364

6465
The above code connects to localhost on port 6379, sets a value in Redis, and retrieves it. All responses are returned as bytes in Python, to receive decoded strings, set *decode_responses=True*. For this, and more connection options, see [these examples](https://redis.readthedocs.io/en/stable/examples.html).
6566

67+
68+
#### RESP3 Support
69+
To enable support for RESP3, ensure you have at least version 5.0 of the client, and change your connection object to include *protocol=3*
70+
71+
``` python
72+
>>> import redis
73+
>>> r = redis.Redis(host='localhost', port=6379, db=0, protocol=3)
74+
```
75+
6676
### Connection Pools
6777

6878
By default, redis-py uses a connection pool to manage connections. Each instance of a Redis class receives its own connection pool. You can however define your own [redis.ConnectionPool](https://redis.readthedocs.io/en/stable/connections.html#connection-pools).

docs/examples/asyncio_examples.ipynb

Lines changed: 142 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
{
2222
"cell_type": "code",
2323
"execution_count": 1,
24+
"metadata": {
25+
"collapsed": false,
26+
"pycharm": {
27+
"name": "#%%\n"
28+
}
29+
},
2430
"outputs": [
2531
{
2632
"name": "stdout",
@@ -36,29 +42,29 @@
3642
"connection = redis.Redis()\n",
3743
"print(f\"Ping successful: {await connection.ping()}\")\n",
3844
"await connection.close()"
39-
],
40-
"metadata": {
41-
"collapsed": false,
42-
"pycharm": {
43-
"name": "#%%\n"
44-
}
45-
}
45+
]
4646
},
4747
{
4848
"cell_type": "markdown",
49-
"source": [
50-
"If you supply a custom `ConnectionPool` that is supplied to several `Redis` instances, you may want to disconnect the connection pool explicitly. Disconnecting the connection pool simply disconnects all connections hosted in the pool."
51-
],
5249
"metadata": {
5350
"collapsed": false,
5451
"pycharm": {
5552
"name": "#%% md\n"
5653
}
57-
}
54+
},
55+
"source": [
56+
"If you supply a custom `ConnectionPool` that is supplied to several `Redis` instances, you may want to disconnect the connection pool explicitly. Disconnecting the connection pool simply disconnects all connections hosted in the pool."
57+
]
5858
},
5959
{
6060
"cell_type": "code",
6161
"execution_count": 2,
62+
"metadata": {
63+
"collapsed": false,
64+
"pycharm": {
65+
"name": "#%%\n"
66+
}
67+
},
6268
"outputs": [],
6369
"source": [
6470
"import redis.asyncio as redis\n",
@@ -67,16 +73,36 @@
6773
"await connection.close()\n",
6874
"# Or: await connection.close(close_connection_pool=False)\n",
6975
"await connection.connection_pool.disconnect()"
70-
],
76+
]
77+
},
78+
{
79+
"cell_type": "markdown",
80+
"metadata": {},
81+
"source": [
82+
"By default, this library uses version 2 of the RESP protocol. To enable RESP version 3, you will want to set `protocol` to 3"
83+
]
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": null,
88+
"metadata": {},
89+
"outputs": [],
90+
"source": [
91+
"import redis.asyncio as redis\n",
92+
"\n",
93+
"connection = redis.Redis(protocol=3)\n",
94+
"await connection.close()\n",
95+
"await connection.ping()"
96+
]
97+
},
98+
{
99+
"cell_type": "markdown",
71100
"metadata": {
72101
"collapsed": false,
73102
"pycharm": {
74-
"name": "#%%\n"
103+
"name": "#%% md\n"
75104
}
76-
}
77-
},
78-
{
79-
"cell_type": "markdown",
105+
},
80106
"source": [
81107
"## Transactions (Multi/Exec)\n",
82108
"\n",
@@ -85,17 +111,17 @@
85111
"The commands will not be reflected in Redis until execute() is called & awaited.\n",
86112
"\n",
87113
"Usually, when performing a bulk operation, taking advantage of a “transaction” (e.g., Multi/Exec) is to be desired, as it will also add a layer of atomicity to your bulk operation."
88-
],
89-
"metadata": {
90-
"collapsed": false,
91-
"pycharm": {
92-
"name": "#%% md\n"
93-
}
94-
}
114+
]
95115
},
96116
{
97117
"cell_type": "code",
98118
"execution_count": 3,
119+
"metadata": {
120+
"collapsed": false,
121+
"pycharm": {
122+
"name": "#%%\n"
123+
}
124+
},
99125
"outputs": [],
100126
"source": [
101127
"import redis.asyncio as redis\n",
@@ -105,31 +131,31 @@
105131
" ok1, ok2 = await (pipe.set(\"key1\", \"value1\").set(\"key2\", \"value2\").execute())\n",
106132
"assert ok1\n",
107133
"assert ok2"
108-
],
109-
"metadata": {
110-
"collapsed": false,
111-
"pycharm": {
112-
"name": "#%%\n"
113-
}
114-
}
134+
]
115135
},
116136
{
117137
"cell_type": "markdown",
118-
"source": [
119-
"## Pub/Sub Mode\n",
120-
"\n",
121-
"Subscribing to specific channels:"
122-
],
123138
"metadata": {
124139
"collapsed": false,
125140
"pycharm": {
126141
"name": "#%% md\n"
127142
}
128-
}
143+
},
144+
"source": [
145+
"## Pub/Sub Mode\n",
146+
"\n",
147+
"Subscribing to specific channels:"
148+
]
129149
},
130150
{
131151
"cell_type": "code",
132152
"execution_count": 4,
153+
"metadata": {
154+
"collapsed": false,
155+
"pycharm": {
156+
"name": "#%%\n"
157+
}
158+
},
133159
"outputs": [
134160
{
135161
"name": "stdout",
@@ -170,29 +196,29 @@
170196
" await r.publish(\"channel:1\", STOPWORD)\n",
171197
"\n",
172198
" await future"
173-
],
174-
"metadata": {
175-
"collapsed": false,
176-
"pycharm": {
177-
"name": "#%%\n"
178-
}
179-
}
199+
]
180200
},
181201
{
182202
"cell_type": "markdown",
183-
"source": [
184-
"Subscribing to channels matching a glob-style pattern:"
185-
],
186203
"metadata": {
187204
"collapsed": false,
188205
"pycharm": {
189206
"name": "#%% md\n"
190207
}
191-
}
208+
},
209+
"source": [
210+
"Subscribing to channels matching a glob-style pattern:"
211+
]
192212
},
193213
{
194214
"cell_type": "code",
195215
"execution_count": 5,
216+
"metadata": {
217+
"collapsed": false,
218+
"pycharm": {
219+
"name": "#%%\n"
220+
}
221+
},
196222
"outputs": [
197223
{
198224
"name": "stdout",
@@ -234,16 +260,16 @@
234260
" await r.publish(\"channel:1\", STOPWORD)\n",
235261
"\n",
236262
" await future"
237-
],
263+
]
264+
},
265+
{
266+
"cell_type": "markdown",
238267
"metadata": {
239268
"collapsed": false,
240269
"pycharm": {
241-
"name": "#%%\n"
270+
"name": "#%% md\n"
242271
}
243-
}
244-
},
245-
{
246-
"cell_type": "markdown",
272+
},
247273
"source": [
248274
"## Sentinel Client\n",
249275
"\n",
@@ -252,17 +278,17 @@
252278
"Calling aioredis.sentinel.Sentinel.master_for or aioredis.sentinel.Sentinel.slave_for methods will return Redis clients connected to specified services monitored by Sentinel.\n",
253279
"\n",
254280
"Sentinel client will detect failover and reconnect Redis clients automatically."
255-
],
256-
"metadata": {
257-
"collapsed": false,
258-
"pycharm": {
259-
"name": "#%% md\n"
260-
}
261-
}
281+
]
262282
},
263283
{
264284
"cell_type": "code",
265285
"execution_count": null,
286+
"metadata": {
287+
"collapsed": false,
288+
"pycharm": {
289+
"name": "#%%\n"
290+
}
291+
},
266292
"outputs": [],
267293
"source": [
268294
"import asyncio\n",
@@ -277,13 +303,61 @@
277303
"assert ok\n",
278304
"val = await r.get(\"key\")\n",
279305
"assert val == b\"value\""
280-
],
281-
"metadata": {
282-
"collapsed": false,
283-
"pycharm": {
284-
"name": "#%%\n"
306+
]
307+
},
308+
{
309+
"cell_type": "markdown",
310+
"metadata": {},
311+
"source": [
312+
"## Connecting to Redis instances by specifying a URL scheme.\n",
313+
"Parameters are passed to the following schems, as parameters to the url scheme.\n",
314+
"\n",
315+
"Three URL schemes are supported:\n",
316+
"\n",
317+
"- `redis://` creates a TCP socket connection. <https://www.iana.org/assignments/uri-schemes/prov/redis>\n",
318+
"- `rediss://` creates a SSL wrapped TCP socket connection. <https://www.iana.org/assignments/uri-schemes/prov/rediss>\n",
319+
"- ``unix://``: creates a Unix Domain Socket connection.\n"
320+
]
321+
},
322+
{
323+
"cell_type": "code",
324+
"execution_count": null,
325+
"metadata": {},
326+
"outputs": [
327+
{
328+
"data": {
329+
"text/plain": [
330+
"True"
331+
]
332+
},
333+
"metadata": {},
334+
"output_type": "display_data"
285335
}
286-
}
336+
],
337+
"source": [
338+
"import redis.asyncio as redis\n",
339+
"url_connection = redis.from_url(\"redis://localhost:6379?decode_responses=True\")\n",
340+
"url_connection.ping()"
341+
]
342+
},
343+
{
344+
"cell_type": "markdown",
345+
"metadata": {},
346+
"source": [
347+
"To enable the RESP 3 protocol, append `protocol=3` to the URL."
348+
]
349+
},
350+
{
351+
"cell_type": "code",
352+
"execution_count": null,
353+
"metadata": {},
354+
"outputs": [],
355+
"source": [
356+
"import redis.asyncio as redis\n",
357+
"\n",
358+
"url_connection = redis.from_url(\"redis://localhost:6379?decode_responses=Trueprotocol=3\")\n",
359+
"url_connection.ping()"
360+
]
287361
}
288362
],
289363
"metadata": {
@@ -307,4 +381,4 @@
307381
},
308382
"nbformat": 4,
309383
"nbformat_minor": 1
310-
}
384+
}

0 commit comments

Comments
 (0)