Skip to content

Commit 2c4b66d

Browse files
committed
pipelines need the response_callback love, too. grammar fixes in docs
1 parent 47a4a7e commit 2c4b66d

File tree

3 files changed

+46
-26
lines changed

3 files changed

+46
-26
lines changed

README.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ are managed.
4343

4444
ConnectionPools manage a set of Connection instances. redis-py ships with two
4545
types of Connections. The default, Connection, is a normal TCP socket based
46-
connection. UnixDomainSocketConnection allows for clients running on the same
47-
device to connect via a unix domain socket. To use a
46+
connection. The UnixDomainSocketConnection allows for clients running on the
47+
same device as the server to connect via a unix domain socket. To use a
4848
UnixDomainSocketConnection connection, simply pass the class to the
4949
connection_class argument of either the Redis or ConnectionPool class. You must
5050
also specify the path argument, which is a string to the unix domain socket
@@ -60,15 +60,16 @@ useful if you want to control the socket behavior within an async framework.
6060
### Parsers
6161

6262
Parser classes provide a way to control how responses from the Redis server
63-
are parsed. redis-py ships with two parse classes, the PythonParse and the
63+
are parsed. redis-py ships with two parser classes, the PythonParser and the
6464
HiredisParser. By default, redis-py will attempt to use the HiredisParser if
6565
you have the hiredis module installed and will fallback to the PythonParser
6666
otherwise.
6767

6868
Hiredis is a C library maintained by the core Redis team. Pieter Noordhuis was
6969
kind enough to create Python bindings. Using Hiredis can provide up to a
70-
10x speed improvement. The performance increase is most noticeable when
71-
retrieving many pieces of data, such as from a ZRANGE or HGETALL operation.
70+
10x speed improvement in parsing responses from the Redis server. The
71+
performance increase is most noticeable when retrieving many pieces of data,
72+
such as from LRANGE or SMEMBERS operations.
7273

7374
Hiredis is available on Pypi, and can be installed via pip or easy_install
7475
just like redis-py.
@@ -89,14 +90,14 @@ Custom callbacks can be added on a per-instance basis using the
8990
set_response_callback method. This method accepts two arguments: a command
9091
name and the callback. Callbacks added in this manner are only valid on the
9192
instance the callback is added to. If you want to define or override a callback
92-
globally, then you should look into making a subclass and added your callback
93-
to the REDIS_CALLBACKS class dictionary.
93+
globally, you should make a subclass of the Redis client and add your callback
94+
to its REDIS_CALLBACKS class dictionary.
9495

9596
Response callbacks take at least one parameter: the response from the Redis
9697
server. Keyword arguments may also be accepted in order to further control
97-
how to interpret the response. The keyword arguments are specified during the
98+
how to interpret the response. These keyword arguments are specified during the
9899
command's call to execute_command. The ZRANGE implementation demonstrates the
99-
use of response callback keyword arguments using the "withscores" argument.
100+
use of response callback keyword arguments with its "withscores" argument.
100101

101102
## Thread Safety
102103

@@ -106,19 +107,15 @@ command execution, and returned to the pool directly after. Command execution
106107
never modifies state on the client instance.
107108

108109
However, there is one caveat: the Redis SELECT command. The SELECT command
109-
allows you to switch to a separate database on the same Redis server. That
110-
database remains selected until another is selected. This creates a proble in
111-
that connections could be returned to the pool that are now set to a different
112-
database.
110+
allows you to switch the database currently in use by the connection. That
111+
database remains selected until another is selected or until the connection is
112+
closed. This creates an issue in that connections could be returned to the pool
113+
that are connected to a different database.
113114

114115
As a result, redis-py does not implement the SELECT command on client instances.
115-
If you use multiple Redis databases, you should create a separate client
116-
instance (and possible a separate connection pool) to each database.
117-
118-
## Versioning scheme
119-
120-
redis-py is versioned after Redis. So, for example, redis-py 2.0.0 should
121-
support all the commands available in Redis 2.0.0.
116+
If you use multiple Redis databases within the same application, you should
117+
create a separate client instance (and possibly a separate connection pool) for
118+
each database.
122119

123120
## API Reference
124121

@@ -131,20 +128,24 @@ arguments as the official spec. There are a few exceptions noted here:
131128
* ZADD: Redis specifies the 'score' argument before 'value'. These were swapped
132129
accidentally when being implemented and not discovered until after people
133130
were already using it. As of Redis 2.4, ZADD will start supporting variable
134-
arguments. redis-py implements these as python keyword arguments, where the
131+
arguments. redis-py implements these as python keyword arguments where the
135132
name is the 'value' and the value is the 'score'.
136133
* DEL: 'del' is a reserved keyword in the Python syntax. Therefore redis-py
137134
uses 'delete' instead.
138-
* CONFIG GET|SET: These are implemented separately config_get or config_set.
135+
* CONFIG GET|SET: These are implemented separately as config_get or config_set.
139136
* MULTI/EXEC: These are implemented as part of the Pipeline class. Calling
140137
the pipeline method and specifying use_transaction=True will cause the
141-
pipline to be wrapped with the MULTI and EXEC statements when it is executed.
138+
pipeline to be wrapped with the MULTI and EXEC statements when it is executed.
142139
* SUBSCRIBE/LISTEN: Similar to pipelines, PubSub is implemented as a separate
143140
class as it places the underlying connection in a state where it can't
144141
execute non-pubsub commands. Calling the pubsub method from the Redis client
145142
will return a PubSub instance where you can subscribe to channels and listen
146143
for messages. You can call PUBLISH from both classes.
147144

145+
## Versioning scheme
146+
147+
redis-py is versioned after Redis. For example, redis-py 2.0.0 should
148+
support all the commands available in Redis 2.0.0.
148149

149150
Author
150151
------

redis/client.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,11 @@ def pipeline(self, transaction=True, shard_hint=None):
196196
atomic, pipelines are useful for reducing the back-and-forth overhead
197197
between the client and server.
198198
"""
199-
return Pipeline(self.connection_pool, transaction, shard_hint)
199+
return Pipeline(
200+
self.connection_pool,
201+
self.response_callbacks,
202+
transaction,
203+
shard_hint)
200204

201205
def lock(self, name, timeout=None, sleep=0.1):
202206
"""
@@ -1170,8 +1174,10 @@ class Pipeline(Redis):
11701174
ResponseError exceptions, such as those raised when issuing a command
11711175
on a key of a different datatype.
11721176
"""
1173-
def __init__(self, connection_pool, transaction, shard_hint):
1177+
def __init__(self, connection_pool, response_callbacks, transaction,
1178+
shard_hint):
11741179
self.connection_pool = connection_pool
1180+
self.response_callbacks = response_callbacks
11751181
self.transaction = transaction
11761182
self.shard_hint = shard_hint
11771183
self.reset()

tests/server_commands.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ def tearDown(self):
1818
self.client.flushdb()
1919
self.client.connection_pool.disconnect()
2020

21+
def test_response_callbacks(self):
22+
self.assertEquals(
23+
self.client.response_callbacks,
24+
redis.Redis.RESPONSE_CALLBACKS)
25+
self.assertNotEquals(
26+
id(self.client.response_callbacks),
27+
id(redis.Redis.RESPONSE_CALLBACKS))
28+
self.client.set_response_callback('GET', lambda x: 'static')
29+
self.client.set('a', 'foo')
30+
self.assertEquals(self.client.get('a'), 'static')
31+
2132
# GENERAL SERVER COMMANDS
2233
def test_dbsize(self):
2334
self.client['a'] = 'foo'
@@ -35,7 +46,9 @@ def test_get_and_set(self):
3546
self.assert_(self.client.set('unicode_string', unicode_string))
3647
self.assertEquals(self.client.get('byte_string'), byte_string)
3748
self.assertEquals(self.client.get('integer'), str(integer))
38-
self.assertEquals(self.client.get('unicode_string').decode('utf-8'), unicode_string)
49+
self.assertEquals(
50+
self.client.get('unicode_string').decode('utf-8'),
51+
unicode_string)
3952

4053
def test_getitem_and_setitem(self):
4154
self.client['a'] = 'bar'

0 commit comments

Comments
 (0)