METADATA 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. Metadata-Version: 2.1
  2. Name: redis
  3. Version: 3.0.1
  4. Summary: Python client for Redis key-value store
  5. Home-page: https://github.com/andymccurdy/redis-py
  6. Author: Andy McCurdy
  7. Author-email: sedrik@gmail.com
  8. Maintainer: Andy McCurdy
  9. Maintainer-email: sedrik@gmail.com
  10. License: MIT
  11. Keywords: Redis,key-value store
  12. Platform: UNKNOWN
  13. Classifier: Development Status :: 5 - Production/Stable
  14. Classifier: Environment :: Console
  15. Classifier: Intended Audience :: Developers
  16. Classifier: License :: OSI Approved :: MIT License
  17. Classifier: Operating System :: OS Independent
  18. Classifier: Programming Language :: Python
  19. Classifier: Programming Language :: Python :: 2
  20. Classifier: Programming Language :: Python :: 2.7
  21. Classifier: Programming Language :: Python :: 3
  22. Classifier: Programming Language :: Python :: 3.4
  23. Classifier: Programming Language :: Python :: 3.5
  24. Classifier: Programming Language :: Python :: 3.6
  25. Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
  26. Description-Content-Type: text/x-rst
  27. Provides-Extra: hiredis
  28. Requires-Dist: hiredis (>=0.1.3); extra == 'hiredis'
  29. redis-py
  30. ========
  31. The Python interface to the Redis key-value store.
  32. .. image:: https://secure.travis-ci.org/andymccurdy/redis-py.png?branch=master
  33. :target: https://travis-ci.org/andymccurdy/redis-py
  34. .. image:: https://readthedocs.org/projects/redis-py/badge/?version=latest&style=flat
  35. :target: https://redis-py.readthedocs.io/en/latest/
  36. .. image:: https://badge.fury.io/py/redis.svg
  37. :target: https://pypi.org/project/redis/
  38. Installation
  39. ------------
  40. redis-py requires a running Redis server. See `Redis's quickstart
  41. <https://redis.io/topics/quickstart>`_ for installation instructions.
  42. redis-py can be installed using `pip` similar to other Python packages. Do not use `sudo`
  43. with `pip`. It is usually good to work in a
  44. `virtualenv <https://virtualenv.pypa.io/en/latest/>`_ or
  45. `venv <https://docs.python.org/3/library/venv.html>`_ to avoid conflicts with other package
  46. managers and Python projects. For a quick introduction see
  47. `Python Virtual Environments in Five Minutes <https://bit.ly/py-env>`_.
  48. To install redis-py, simply:
  49. .. code-block:: bash
  50. $ pip install redis
  51. or from source:
  52. .. code-block:: bash
  53. $ python setup.py install
  54. Getting Started
  55. ---------------
  56. .. code-block:: pycon
  57. >>> import redis
  58. >>> r = redis.Redis(host='localhost', port=6379, db=0)
  59. >>> r.set('foo', 'bar')
  60. True
  61. >>> r.get('foo')
  62. 'bar'
  63. By default, all responses are returned as `bytes` in Python 3 and `str` in
  64. Python 2. The user is responsible for decoding to Python 3 strings or Python 2
  65. unicode objects.
  66. If **all** string responses from a client should be decoded, the user can
  67. specify `decode_responses=True` to `Redis.__init__`. In this case, any
  68. Redis command that returns a string type will be decoded with the `encoding`
  69. specified.
  70. Upgrading from redis-py 2.X to 3.0
  71. ----------------------------------
  72. redis-py 3.0 introduces many new features but required a number of backwards
  73. incompatible changes to be made in the process. This section attempts to
  74. provide an upgrade path for users migrating from 2.X to 3.0.
  75. Python Version Support
  76. ^^^^^^^^^^^^^^^^^^^^^^
  77. redis-py 3.0 now supports Python 2.7 and Python 3.4+. Python 2.6 and 3.3
  78. support has been dropped.
  79. Client Classes: Redis and StrictRedis
  80. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  81. redis-py 3.0 drops support for the legacy "Redis" client class. "StrictRedis"
  82. has been renamed to "Redis" and an alias named "StrictRedis" is provided so
  83. that users previously using "StrictRedis" can continue to run unchanged.
  84. The 2.X "Redis" class provided alternative implementations of a few commands.
  85. This confused users (rightfully so) and caused a number of support issues. To
  86. make things easier going forward, it was decided to drop support for these
  87. alternate implementations and instead focus on a single client class.
  88. 2.X users that are already using StrictRedis don't have to change the class
  89. name. StrictRedis will continue to work for the forseeable future.
  90. 2.X users that are using the Redis class will have to make changes if they
  91. use any of the following commands:
  92. * SETEX: The argument order has changed. The new order is (name, time, value).
  93. * LREM: The argument order has changed. The new order is (name, num, value).
  94. * TTL and PTTL: The return value is now always an int and matches the
  95. official Redis command (>0 indicates the timeout, -1 indicates that the key
  96. exists but that it has no expire time set, -2 indicates that the key does
  97. not exist)
  98. MSET, MSETNX and ZADD
  99. ^^^^^^^^^^^^^^^^^^^^^
  100. These commands all accept a mapping of key/value pairs. In redis-py 2.X
  101. this mapping could be specified as ``*args`` or as ``**kwargs``. Both of these
  102. styles caused issues when Redis introduced optional flags to ZADD. Relying on
  103. ``*args`` caused issues with the optional argument order, especially in Python
  104. 2.7. Relying on ``**kwargs`` caused potential collision issues of user keys with
  105. the argument names in the method signature.
  106. To resolve this, redis-py 3.0 has changed these three commands to all accept
  107. a single positional argument named mapping that is expected to be a dict. For
  108. MSET and MSETNX, the dict is a mapping of key-names -> values. For ZADD, the
  109. dict is a mapping of element-names -> score.
  110. MSET, MSETNX and ZADD now look like:
  111. .. code-block:: pycon
  112. def mset(self, mapping):
  113. def msetnx(self, mapping):
  114. def zadd(self, name, mapping, nx=False, xx=False, ch=False, incr=False):
  115. All 2.X users that use these commands must modify their code to supply
  116. keys and values as a dict to these commands.
  117. ZINCRBY
  118. ^^^^^^^
  119. redis-py 2.X accidentily modified the argument order of ZINCRBY, swapping the
  120. order of value and amount. ZINCRBY now looks like:
  121. .. code-block:: pycon
  122. def zincrby(self, name, amount, value):
  123. All 2.X users that rely on ZINCRBY must swap the order of amount and value
  124. for the command to continue to work as intended.
  125. Encoding of User Input
  126. ^^^^^^^^^^^^^^^^^^^^^^
  127. redis-py 3.0 only accepts user data as bytes, strings or numbers (ints, longs
  128. and floats). Attempting to specify a key or a value as any other type will
  129. raise a DataError exception.
  130. redis-py 2.X attempted to coerce any type of input into a string. While
  131. occasionally convenient, this caused all sorts of hidden errors when users
  132. passed boolean values (which were coerced to 'True' or 'False'), a None
  133. value (which was coerced to 'None') or other values, such as user defined
  134. types.
  135. All 2.X users should make sure that the keys and values they pass into
  136. redis-py are either bytes, strings or numbers.
  137. Locks
  138. ^^^^^
  139. redis-py 3.0 drops support for the pipeline-based Lock and now only supports
  140. the Lua-based lock. In doing so, LuaLock has been renamed to Lock. This also
  141. means that redis-py Lock objects require Redis server 2.6 or greater.
  142. 2.X users that were explicitly referring to "LuaLock" will have to now refer
  143. to "Lock" instead.
  144. Locks as Context Managers
  145. ^^^^^^^^^^^^^^^^^^^^^^^^^
  146. redis-py 3.0 now raises a LockError when using a lock as a context manager and
  147. the lock cannot be acquired within the specified timeout. This is more of a
  148. bug fix than a backwards incompatible change. However, given an error is now
  149. raised where none was before, this might alarm some users.
  150. 2.X users should make sure they're wrapping their lock code in a try/catch
  151. like this:
  152. .. code-block:: pycon
  153. try:
  154. with r.lock('my-lock-key', blocking_timeout=5) as lock:
  155. # code you want executed only after the lock has been acquired
  156. except LockError:
  157. # the lock wasn't acquired
  158. API Reference
  159. -------------
  160. The `official Redis command documentation <https://redis.io/commands>`_ does a
  161. great job of explaining each command in detail. redis-py attempts to adhere
  162. to the official command syntax. There are a few exceptions:
  163. * **SELECT**: Not implemented. See the explanation in the Thread Safety section
  164. below.
  165. * **DEL**: 'del' is a reserved keyword in the Python syntax. Therefore redis-py
  166. uses 'delete' instead.
  167. * **MULTI/EXEC**: These are implemented as part of the Pipeline class. The
  168. pipeline is wrapped with the MULTI and EXEC statements by default when it
  169. is executed, which can be disabled by specifying transaction=False.
  170. See more about Pipelines below.
  171. * **SUBSCRIBE/LISTEN**: Similar to pipelines, PubSub is implemented as a separate
  172. class as it places the underlying connection in a state where it can't
  173. execute non-pubsub commands. Calling the pubsub method from the Redis client
  174. will return a PubSub instance where you can subscribe to channels and listen
  175. for messages. You can only call PUBLISH from the Redis client (see
  176. `this comment on issue #151
  177. <https://github.com/andymccurdy/redis-py/issues/151#issuecomment-1545015>`_
  178. for details).
  179. * **SCAN/SSCAN/HSCAN/ZSCAN**: The \*SCAN commands are implemented as they
  180. exist in the Redis documentation. In addition, each command has an equivilant
  181. iterator method. These are purely for convenience so the user doesn't have
  182. to keep track of the cursor while iterating. Use the
  183. scan_iter/sscan_iter/hscan_iter/zscan_iter methods for this behavior.
  184. More Detail
  185. -----------
  186. Connection Pools
  187. ^^^^^^^^^^^^^^^^
  188. Behind the scenes, redis-py uses a connection pool to manage connections to
  189. a Redis server. By default, each Redis instance you create will in turn create
  190. its own connection pool. You can override this behavior and use an existing
  191. connection pool by passing an already created connection pool instance to the
  192. connection_pool argument of the Redis class. You may choose to do this in order
  193. to implement client side sharding or have finer grain control of how
  194. connections are managed.
  195. .. code-block:: pycon
  196. >>> pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
  197. >>> r = redis.Redis(connection_pool=pool)
  198. Connections
  199. ^^^^^^^^^^^
  200. ConnectionPools manage a set of Connection instances. redis-py ships with two
  201. types of Connections. The default, Connection, is a normal TCP socket based
  202. connection. The UnixDomainSocketConnection allows for clients running on the
  203. same device as the server to connect via a unix domain socket. To use a
  204. UnixDomainSocketConnection connection, simply pass the unix_socket_path
  205. argument, which is a string to the unix domain socket file. Additionally, make
  206. sure the unixsocket parameter is defined in your redis.conf file. It's
  207. commented out by default.
  208. .. code-block:: pycon
  209. >>> r = redis.Redis(unix_socket_path='/tmp/redis.sock')
  210. You can create your own Connection subclasses as well. This may be useful if
  211. you want to control the socket behavior within an async framework. To
  212. instantiate a client class using your own connection, you need to create
  213. a connection pool, passing your class to the connection_class argument.
  214. Other keyword parameters you pass to the pool will be passed to the class
  215. specified during initialization.
  216. .. code-block:: pycon
  217. >>> pool = redis.ConnectionPool(connection_class=YourConnectionClass,
  218. your_arg='...', ...)
  219. Parsers
  220. ^^^^^^^
  221. Parser classes provide a way to control how responses from the Redis server
  222. are parsed. redis-py ships with two parser classes, the PythonParser and the
  223. HiredisParser. By default, redis-py will attempt to use the HiredisParser if
  224. you have the hiredis module installed and will fallback to the PythonParser
  225. otherwise.
  226. Hiredis is a C library maintained by the core Redis team. Pieter Noordhuis was
  227. kind enough to create Python bindings. Using Hiredis can provide up to a
  228. 10x speed improvement in parsing responses from the Redis server. The
  229. performance increase is most noticeable when retrieving many pieces of data,
  230. such as from LRANGE or SMEMBERS operations.
  231. Hiredis is available on PyPI, and can be installed via pip just like redis-py.
  232. .. code-block:: bash
  233. $ pip install hiredis
  234. Response Callbacks
  235. ^^^^^^^^^^^^^^^^^^
  236. The client class uses a set of callbacks to cast Redis responses to the
  237. appropriate Python type. There are a number of these callbacks defined on
  238. the Redis client class in a dictionary called RESPONSE_CALLBACKS.
  239. Custom callbacks can be added on a per-instance basis using the
  240. set_response_callback method. This method accepts two arguments: a command
  241. name and the callback. Callbacks added in this manner are only valid on the
  242. instance the callback is added to. If you want to define or override a callback
  243. globally, you should make a subclass of the Redis client and add your callback
  244. to its RESPONSE_CALLBACKS class dictionary.
  245. Response callbacks take at least one parameter: the response from the Redis
  246. server. Keyword arguments may also be accepted in order to further control
  247. how to interpret the response. These keyword arguments are specified during the
  248. command's call to execute_command. The ZRANGE implementation demonstrates the
  249. use of response callback keyword arguments with its "withscores" argument.
  250. Thread Safety
  251. ^^^^^^^^^^^^^
  252. Redis client instances can safely be shared between threads. Internally,
  253. connection instances are only retrieved from the connection pool during
  254. command execution, and returned to the pool directly after. Command execution
  255. never modifies state on the client instance.
  256. However, there is one caveat: the Redis SELECT command. The SELECT command
  257. allows you to switch the database currently in use by the connection. That
  258. database remains selected until another is selected or until the connection is
  259. closed. This creates an issue in that connections could be returned to the pool
  260. that are connected to a different database.
  261. As a result, redis-py does not implement the SELECT command on client
  262. instances. If you use multiple Redis databases within the same application, you
  263. should create a separate client instance (and possibly a separate connection
  264. pool) for each database.
  265. It is not safe to pass PubSub or Pipeline objects between threads.
  266. Pipelines
  267. ^^^^^^^^^
  268. Pipelines are a subclass of the base Redis class that provide support for
  269. buffering multiple commands to the server in a single request. They can be used
  270. to dramatically increase the performance of groups of commands by reducing the
  271. number of back-and-forth TCP packets between the client and server.
  272. Pipelines are quite simple to use:
  273. .. code-block:: pycon
  274. >>> r = redis.Redis(...)
  275. >>> r.set('bing', 'baz')
  276. >>> # Use the pipeline() method to create a pipeline instance
  277. >>> pipe = r.pipeline()
  278. >>> # The following SET commands are buffered
  279. >>> pipe.set('foo', 'bar')
  280. >>> pipe.get('bing')
  281. >>> # the EXECUTE call sends all buffered commands to the server, returning
  282. >>> # a list of responses, one for each command.
  283. >>> pipe.execute()
  284. [True, 'baz']
  285. For ease of use, all commands being buffered into the pipeline return the
  286. pipeline object itself. Therefore calls can be chained like:
  287. .. code-block:: pycon
  288. >>> pipe.set('foo', 'bar').sadd('faz', 'baz').incr('auto_number').execute()
  289. [True, True, 6]
  290. In addition, pipelines can also ensure the buffered commands are executed
  291. atomically as a group. This happens by default. If you want to disable the
  292. atomic nature of a pipeline but still want to buffer commands, you can turn
  293. off transactions.
  294. .. code-block:: pycon
  295. >>> pipe = r.pipeline(transaction=False)
  296. A common issue occurs when requiring atomic transactions but needing to
  297. retrieve values in Redis prior for use within the transaction. For instance,
  298. let's assume that the INCR command didn't exist and we need to build an atomic
  299. version of INCR in Python.
  300. The completely naive implementation could GET the value, increment it in
  301. Python, and SET the new value back. However, this is not atomic because
  302. multiple clients could be doing this at the same time, each getting the same
  303. value from GET.
  304. Enter the WATCH command. WATCH provides the ability to monitor one or more keys
  305. prior to starting a transaction. If any of those keys change prior the
  306. execution of that transaction, the entire transaction will be canceled and a
  307. WatchError will be raised. To implement our own client-side INCR command, we
  308. could do something like this:
  309. .. code-block:: pycon
  310. >>> with r.pipeline() as pipe:
  311. ... while True:
  312. ... try:
  313. ... # put a WATCH on the key that holds our sequence value
  314. ... pipe.watch('OUR-SEQUENCE-KEY')
  315. ... # after WATCHing, the pipeline is put into immediate execution
  316. ... # mode until we tell it to start buffering commands again.
  317. ... # this allows us to get the current value of our sequence
  318. ... current_value = pipe.get('OUR-SEQUENCE-KEY')
  319. ... next_value = int(current_value) + 1
  320. ... # now we can put the pipeline back into buffered mode with MULTI
  321. ... pipe.multi()
  322. ... pipe.set('OUR-SEQUENCE-KEY', next_value)
  323. ... # and finally, execute the pipeline (the set command)
  324. ... pipe.execute()
  325. ... # if a WatchError wasn't raised during execution, everything
  326. ... # we just did happened atomically.
  327. ... break
  328. ... except WatchError:
  329. ... # another client must have changed 'OUR-SEQUENCE-KEY' between
  330. ... # the time we started WATCHing it and the pipeline's execution.
  331. ... # our best bet is to just retry.
  332. ... continue
  333. Note that, because the Pipeline must bind to a single connection for the
  334. duration of a WATCH, care must be taken to ensure that the connection is
  335. returned to the connection pool by calling the reset() method. If the
  336. Pipeline is used as a context manager (as in the example above) reset()
  337. will be called automatically. Of course you can do this the manual way by
  338. explicitly calling reset():
  339. .. code-block:: pycon
  340. >>> pipe = r.pipeline()
  341. >>> while True:
  342. ... try:
  343. ... pipe.watch('OUR-SEQUENCE-KEY')
  344. ... ...
  345. ... pipe.execute()
  346. ... break
  347. ... except WatchError:
  348. ... continue
  349. ... finally:
  350. ... pipe.reset()
  351. A convenience method named "transaction" exists for handling all the
  352. boilerplate of handling and retrying watch errors. It takes a callable that
  353. should expect a single parameter, a pipeline object, and any number of keys to
  354. be WATCHed. Our client-side INCR command above can be written like this,
  355. which is much easier to read:
  356. .. code-block:: pycon
  357. >>> def client_side_incr(pipe):
  358. ... current_value = pipe.get('OUR-SEQUENCE-KEY')
  359. ... next_value = int(current_value) + 1
  360. ... pipe.multi()
  361. ... pipe.set('OUR-SEQUENCE-KEY', next_value)
  362. >>>
  363. >>> r.transaction(client_side_incr, 'OUR-SEQUENCE-KEY')
  364. [True]
  365. Publish / Subscribe
  366. ^^^^^^^^^^^^^^^^^^^
  367. redis-py includes a `PubSub` object that subscribes to channels and listens
  368. for new messages. Creating a `PubSub` object is easy.
  369. .. code-block:: pycon
  370. >>> r = redis.Redis(...)
  371. >>> p = r.pubsub()
  372. Once a `PubSub` instance is created, channels and patterns can be subscribed
  373. to.
  374. .. code-block:: pycon
  375. >>> p.subscribe('my-first-channel', 'my-second-channel', ...)
  376. >>> p.psubscribe('my-*', ...)
  377. The `PubSub` instance is now subscribed to those channels/patterns. The
  378. subscription confirmations can be seen by reading messages from the `PubSub`
  379. instance.
  380. .. code-block:: pycon
  381. >>> p.get_message()
  382. {'pattern': None, 'type': 'subscribe', 'channel': 'my-second-channel', 'data': 1L}
  383. >>> p.get_message()
  384. {'pattern': None, 'type': 'subscribe', 'channel': 'my-first-channel', 'data': 2L}
  385. >>> p.get_message()
  386. {'pattern': None, 'type': 'psubscribe', 'channel': 'my-*', 'data': 3L}
  387. Every message read from a `PubSub` instance will be a dictionary with the
  388. following keys.
  389. * **type**: One of the following: 'subscribe', 'unsubscribe', 'psubscribe',
  390. 'punsubscribe', 'message', 'pmessage'
  391. * **channel**: The channel [un]subscribed to or the channel a message was
  392. published to
  393. * **pattern**: The pattern that matched a published message's channel. Will be
  394. `None` in all cases except for 'pmessage' types.
  395. * **data**: The message data. With [un]subscribe messages, this value will be
  396. the number of channels and patterns the connection is currently subscribed
  397. to. With [p]message messages, this value will be the actual published
  398. message.
  399. Let's send a message now.
  400. .. code-block:: pycon
  401. # the publish method returns the number matching channel and pattern
  402. # subscriptions. 'my-first-channel' matches both the 'my-first-channel'
  403. # subscription and the 'my-*' pattern subscription, so this message will
  404. # be delivered to 2 channels/patterns
  405. >>> r.publish('my-first-channel', 'some data')
  406. 2
  407. >>> p.get_message()
  408. {'channel': 'my-first-channel', 'data': 'some data', 'pattern': None, 'type': 'message'}
  409. >>> p.get_message()
  410. {'channel': 'my-first-channel', 'data': 'some data', 'pattern': 'my-*', 'type': 'pmessage'}
  411. Unsubscribing works just like subscribing. If no arguments are passed to
  412. [p]unsubscribe, all channels or patterns will be unsubscribed from.
  413. .. code-block:: pycon
  414. >>> p.unsubscribe()
  415. >>> p.punsubscribe('my-*')
  416. >>> p.get_message()
  417. {'channel': 'my-second-channel', 'data': 2L, 'pattern': None, 'type': 'unsubscribe'}
  418. >>> p.get_message()
  419. {'channel': 'my-first-channel', 'data': 1L, 'pattern': None, 'type': 'unsubscribe'}
  420. >>> p.get_message()
  421. {'channel': 'my-*', 'data': 0L, 'pattern': None, 'type': 'punsubscribe'}
  422. redis-py also allows you to register callback functions to handle published
  423. messages. Message handlers take a single argument, the message, which is a
  424. dictionary just like the examples above. To subscribe to a channel or pattern
  425. with a message handler, pass the channel or pattern name as a keyword argument
  426. with its value being the callback function.
  427. When a message is read on a channel or pattern with a message handler, the
  428. message dictionary is created and passed to the message handler. In this case,
  429. a `None` value is returned from get_message() since the message was already
  430. handled.
  431. .. code-block:: pycon
  432. >>> def my_handler(message):
  433. ... print 'MY HANDLER: ', message['data']
  434. >>> p.subscribe(**{'my-channel': my_handler})
  435. # read the subscribe confirmation message
  436. >>> p.get_message()
  437. {'pattern': None, 'type': 'subscribe', 'channel': 'my-channel', 'data': 1L}
  438. >>> r.publish('my-channel', 'awesome data')
  439. 1
  440. # for the message handler to work, we need tell the instance to read data.
  441. # this can be done in several ways (read more below). we'll just use
  442. # the familiar get_message() function for now
  443. >>> message = p.get_message()
  444. MY HANDLER: awesome data
  445. # note here that the my_handler callback printed the string above.
  446. # `message` is None because the message was handled by our handler.
  447. >>> print message
  448. None
  449. If your application is not interested in the (sometimes noisy)
  450. subscribe/unsubscribe confirmation messages, you can ignore them by passing
  451. `ignore_subscribe_messages=True` to `r.pubsub()`. This will cause all
  452. subscribe/unsubscribe messages to be read, but they won't bubble up to your
  453. application.
  454. .. code-block:: pycon
  455. >>> p = r.pubsub(ignore_subscribe_messages=True)
  456. >>> p.subscribe('my-channel')
  457. >>> p.get_message() # hides the subscribe message and returns None
  458. >>> r.publish('my-channel')
  459. 1
  460. >>> p.get_message()
  461. {'channel': 'my-channel', 'data': 'my data', 'pattern': None, 'type': 'message'}
  462. There are three different strategies for reading messages.
  463. The examples above have been using `pubsub.get_message()`. Behind the scenes,
  464. `get_message()` uses the system's 'select' module to quickly poll the
  465. connection's socket. If there's data available to be read, `get_message()` will
  466. read it, format the message and return it or pass it to a message handler. If
  467. there's no data to be read, `get_message()` will immediately return None. This
  468. makes it trivial to integrate into an existing event loop inside your
  469. application.
  470. .. code-block:: pycon
  471. >>> while True:
  472. >>> message = p.get_message()
  473. >>> if message:
  474. >>> # do something with the message
  475. >>> time.sleep(0.001) # be nice to the system :)
  476. Older versions of redis-py only read messages with `pubsub.listen()`. listen()
  477. is a generator that blocks until a message is available. If your application
  478. doesn't need to do anything else but receive and act on messages received from
  479. redis, listen() is an easy way to get up an running.
  480. .. code-block:: pycon
  481. >>> for message in p.listen():
  482. ... # do something with the message
  483. The third option runs an event loop in a separate thread.
  484. `pubsub.run_in_thread()` creates a new thread and starts the event loop. The
  485. thread object is returned to the caller of `run_in_thread()`. The caller can
  486. use the `thread.stop()` method to shut down the event loop and thread. Behind
  487. the scenes, this is simply a wrapper around `get_message()` that runs in a
  488. separate thread, essentially creating a tiny non-blocking event loop for you.
  489. `run_in_thread()` takes an optional `sleep_time` argument. If specified, the
  490. event loop will call `time.sleep()` with the value in each iteration of the
  491. loop.
  492. Note: Since we're running in a separate thread, there's no way to handle
  493. messages that aren't automatically handled with registered message handlers.
  494. Therefore, redis-py prevents you from calling `run_in_thread()` if you're
  495. subscribed to patterns or channels that don't have message handlers attached.
  496. .. code-block:: pycon
  497. >>> p.subscribe(**{'my-channel': my_handler})
  498. >>> thread = p.run_in_thread(sleep_time=0.001)
  499. # the event loop is now running in the background processing messages
  500. # when it's time to shut it down...
  501. >>> thread.stop()
  502. A PubSub object adheres to the same encoding semantics as the client instance
  503. it was created from. Any channel or pattern that's unicode will be encoded
  504. using the `charset` specified on the client before being sent to Redis. If the
  505. client's `decode_responses` flag is set the False (the default), the
  506. 'channel', 'pattern' and 'data' values in message dictionaries will be byte
  507. strings (str on Python 2, bytes on Python 3). If the client's
  508. `decode_responses` is True, then the 'channel', 'pattern' and 'data' values
  509. will be automatically decoded to unicode strings using the client's `charset`.
  510. PubSub objects remember what channels and patterns they are subscribed to. In
  511. the event of a disconnection such as a network error or timeout, the
  512. PubSub object will re-subscribe to all prior channels and patterns when
  513. reconnecting. Messages that were published while the client was disconnected
  514. cannot be delivered. When you're finished with a PubSub object, call its
  515. `.close()` method to shutdown the connection.
  516. .. code-block:: pycon
  517. >>> p = r.pubsub()
  518. >>> ...
  519. >>> p.close()
  520. The PUBSUB set of subcommands CHANNELS, NUMSUB and NUMPAT are also
  521. supported:
  522. .. code-block:: pycon
  523. >>> r.pubsub_channels()
  524. ['foo', 'bar']
  525. >>> r.pubsub_numsub('foo', 'bar')
  526. [('foo', 9001), ('bar', 42)]
  527. >>> r.pubsub_numsub('baz')
  528. [('baz', 0)]
  529. >>> r.pubsub_numpat()
  530. 1204
  531. Lua Scripting
  532. ^^^^^^^^^^^^^
  533. redis-py supports the EVAL, EVALSHA, and SCRIPT commands. However, there are
  534. a number of edge cases that make these commands tedious to use in real world
  535. scenarios. Therefore, redis-py exposes a Script object that makes scripting
  536. much easier to use.
  537. To create a Script instance, use the `register_script` function on a client
  538. instance passing the Lua code as the first argument. `register_script` returns
  539. a Script instance that you can use throughout your code.
  540. The following trivial Lua script accepts two parameters: the name of a key and
  541. a multiplier value. The script fetches the value stored in the key, multiplies
  542. it with the multiplier value and returns the result.
  543. .. code-block:: pycon
  544. >>> r = redis.Redis()
  545. >>> lua = """
  546. ... local value = redis.call('GET', KEYS[1])
  547. ... value = tonumber(value)
  548. ... return value * ARGV[1]"""
  549. >>> multiply = r.register_script(lua)
  550. `multiply` is now a Script instance that is invoked by calling it like a
  551. function. Script instances accept the following optional arguments:
  552. * **keys**: A list of key names that the script will access. This becomes the
  553. KEYS list in Lua.
  554. * **args**: A list of argument values. This becomes the ARGV list in Lua.
  555. * **client**: A redis-py Client or Pipeline instance that will invoke the
  556. script. If client isn't specified, the client that intiially
  557. created the Script instance (the one that `register_script` was
  558. invoked from) will be used.
  559. Continuing the example from above:
  560. .. code-block:: pycon
  561. >>> r.set('foo', 2)
  562. >>> multiply(keys=['foo'], args=[5])
  563. 10
  564. The value of key 'foo' is set to 2. When multiply is invoked, the 'foo' key is
  565. passed to the script along with the multiplier value of 5. Lua executes the
  566. script and returns the result, 10.
  567. Script instances can be executed using a different client instance, even one
  568. that points to a completely different Redis server.
  569. .. code-block:: pycon
  570. >>> r2 = redis.Redis('redis2.example.com')
  571. >>> r2.set('foo', 3)
  572. >>> multiply(keys=['foo'], args=[5], client=r2)
  573. 15
  574. The Script object ensures that the Lua script is loaded into Redis's script
  575. cache. In the event of a NOSCRIPT error, it will load the script and retry
  576. executing it.
  577. Script objects can also be used in pipelines. The pipeline instance should be
  578. passed as the client argument when calling the script. Care is taken to ensure
  579. that the script is registered in Redis's script cache just prior to pipeline
  580. execution.
  581. .. code-block:: pycon
  582. >>> pipe = r.pipeline()
  583. >>> pipe.set('foo', 5)
  584. >>> multiply(keys=['foo'], args=[5], client=pipe)
  585. >>> pipe.execute()
  586. [True, 25]
  587. Sentinel support
  588. ^^^^^^^^^^^^^^^^
  589. redis-py can be used together with `Redis Sentinel <https://redis.io/topics/sentinel>`_
  590. to discover Redis nodes. You need to have at least one Sentinel daemon running
  591. in order to use redis-py's Sentinel support.
  592. Connecting redis-py to the Sentinel instance(s) is easy. You can use a
  593. Sentinel connection to discover the master and slaves network addresses:
  594. .. code-block:: pycon
  595. >>> from redis.sentinel import Sentinel
  596. >>> sentinel = Sentinel([('localhost', 26379)], socket_timeout=0.1)
  597. >>> sentinel.discover_master('mymaster')
  598. ('127.0.0.1', 6379)
  599. >>> sentinel.discover_slaves('mymaster')
  600. [('127.0.0.1', 6380)]
  601. You can also create Redis client connections from a Sentinel instance. You can
  602. connect to either the master (for write operations) or a slave (for read-only
  603. operations).
  604. .. code-block:: pycon
  605. >>> master = sentinel.master_for('mymaster', socket_timeout=0.1)
  606. >>> slave = sentinel.slave_for('mymaster', socket_timeout=0.1)
  607. >>> master.set('foo', 'bar')
  608. >>> slave.get('foo')
  609. 'bar'
  610. The master and slave objects are normal Redis instances with their
  611. connection pool bound to the Sentinel instance. When a Sentinel backed client
  612. attempts to establish a connection, it first queries the Sentinel servers to
  613. determine an appropriate host to connect to. If no server is found,
  614. a MasterNotFoundError or SlaveNotFoundError is raised. Both exceptions are
  615. subclasses of ConnectionError.
  616. When trying to connect to a slave client, the Sentinel connection pool will
  617. iterate over the list of slaves until it finds one that can be connected to.
  618. If no slaves can be connected to, a connection will be established with the
  619. master.
  620. See `Guidelines for Redis clients with support for Redis Sentinel
  621. <https://redis.io/topics/sentinel-clients>`_ to learn more about Redis Sentinel.
  622. Scan Iterators
  623. ^^^^^^^^^^^^^^
  624. The \*SCAN commands introduced in Redis 2.8 can be cumbersome to use. While
  625. these commands are fully supported, redis-py also exposes the following methods
  626. that return Python iterators for convenience: `scan_iter`, `hscan_iter`,
  627. `sscan_iter` and `zscan_iter`.
  628. .. code-block:: pycon
  629. >>> for key, value in (('A', '1'), ('B', '2'), ('C', '3')):
  630. ... r.set(key, value)
  631. >>> for key in r.scan_iter():
  632. ... print key, r.get(key)
  633. A 1
  634. B 2
  635. C 3
  636. Author
  637. ^^^^^^
  638. redis-py is developed and maintained by Andy McCurdy (sedrik@gmail.com).
  639. It can be found here: https://github.com/andymccurdy/redis-py
  640. Special thanks to:
  641. * Ludovico Magnocavallo, author of the original Python Redis client, from
  642. which some of the socket code is still used.
  643. * Alexander Solovyov for ideas on the generic response callback system.
  644. * Paul Hubbard for initial packaging support.