@@ -323,24 +323,24 @@ Publish / Subscribe
323
323
^^^^^^^^^^^^^^^^^^^
324
324
325
325
redis-py includes a `PubSub ` object that subscribes to channels and listens
326
- for new messages. Creating a `PubSub ` object is easy:
326
+ for new messages. Creating a `PubSub ` object is easy.
327
327
328
328
.. code-block :: pycon
329
329
330
330
>>> r = redis.StrictRedis(...)
331
331
>>> p = r.pubsub()
332
332
333
333
Once a `PubSub ` instance is created, channels and patterns can be subscribed
334
- to:
334
+ to.
335
335
336
336
.. code-block :: pycon
337
337
338
338
>>> p.subscribe('my-first-channel', 'my-second-channel', ...)
339
339
>>> p.psubscribe('my-*', ...)
340
340
341
341
The `PubSub ` instance is now subscribed to those channels/patterns. The
342
- subscription confirmations can be seen by reading messages off the `PubSub `
343
- instance:
342
+ subscription confirmations can be seen by reading messages from the `PubSub `
343
+ instance.
344
344
345
345
.. code-block :: pycon
346
346
@@ -352,14 +352,14 @@ instance:
352
352
{'pattern': None, 'type': 'psubscribe', 'channel': 'my-*', 'data': 3L}
353
353
354
354
Every message read from a `PubSub ` instance will be a dictionary with the
355
- following keys:
355
+ following keys.
356
356
357
357
* **type **: One of the following: 'subscribe', 'unsubscribe', 'psubscribe',
358
358
'punsubscribe', 'message', 'pmessage'
359
359
* **channel **: The channel [un]subscribed to or the channel a message was
360
360
published to
361
- * **pattern **: The pattern that matched a published messages channel. Will be
362
- None in all cases except for 'pmessage' types.
361
+ * **pattern **: The pattern that matched a published message's channel. Will be
362
+ ` None ` in all cases except for 'pmessage' types.
363
363
* **data **: The message data. With [un]subscribe messages, this value will be
364
364
the number of channels and patterns the connection is currently subscribed
365
365
to. With [p]message messages, this value will be the actual published
@@ -381,7 +381,7 @@ Let's send a message now.
381
381
{'channel': 'my-first-channel', 'data': 'some data', 'pattern': 'my-*', 'type': 'pmessage'}
382
382
383
383
Unsubscribing works just like subscribing. If no arguments are passed to
384
- [p]unsubscribe, all channels or patterns will be unsubscribed from:
384
+ [p]unsubscribe, all channels or patterns will be unsubscribed from.
385
385
386
386
.. code-block :: pycon
387
387
@@ -398,17 +398,19 @@ redis-py also allows you to register callback functions to handle published
398
398
messages. Message handlers take a single argument, the message, which is a
399
399
dictionary just like the examples above. To subscribe to a channel or pattern
400
400
with a message handler, pass the channel or pattern name as a keyword argument
401
- with a value of the callback function.
401
+ with its value being the callback function.
402
402
403
- When a message is read on a channels or pattern with a message handler, the
404
- message dictionary is created and passed to the message handler. A None value
405
- is returned from get_message().
403
+ When a message is read on a channel or pattern with a message handler, the
404
+ message dictionary is created and passed to the message handler. In this case,
405
+ a `None ` value is returned from get_message() since the message was already
406
+ handled.
406
407
407
408
.. code-block :: pycon
408
409
409
410
>>> def my_handler(message):
410
411
... print 'MY HANDLER: ', message['data']
411
412
>>> p.subscribe(**{'my-channel': my_handler})
413
+ # read the subscribe confirmation message
412
414
>>> p.get_message()
413
415
{'pattern': None, 'type': 'subscribe', 'channel': 'my-channel', 'data': 1L}
414
416
>>> r.publish('my-channel', 'awesome data')
@@ -424,9 +426,10 @@ is returned from get_message().
424
426
None
425
427
426
428
If your application is not interested in the (sometimes noisy)
427
- subscribe/unsubscribe messages, you can pass `ignore_subscribe_messages=True `
428
- to redis.pubsub(). This will cause all subscribe/unsubscribe messages to be
429
- read, but they won't bubble up to your application:
429
+ subscribe/unsubscribe confirmation messages, you can ignore them by passing
430
+ `ignore_subscribe_messages=True ` to `r.pubsub() `. This will cause all
431
+ subscribe/unsubscribe messages to be read, but they won't bubble up to your
432
+ application.
430
433
431
434
.. code-block :: pycon
432
435
@@ -445,7 +448,8 @@ The examples above have been using `pubsub.get_message()`. Behind the scenes,
445
448
connection's socket. If there's data available to be read, `get_message() ` will
446
449
read it, format the message and return it or pass it to a message handler. If
447
450
there's no data to be read, `get_message() ` will immediately return None. This
448
- makes it trivial to integrate into an event loop inside your application:
451
+ makes it trivial to integrate into an existing event loop inside your
452
+ application.
449
453
450
454
.. code-block :: pycon
451
455
@@ -458,7 +462,7 @@ makes it trivial to integrate into an event loop inside your application:
458
462
Older versions of redis-py only read messages with `pubsub.listen() `. listen()
459
463
is a generator that blocks until a message is available. If your application
460
464
doesn't need to do anything else but receive and act on messages received from
461
- redis, listen() is an easy way to get up an running:
465
+ redis, listen() is an easy way to get up an running.
462
466
463
467
.. code-block :: pycon
464
468
@@ -467,7 +471,7 @@ redis, listen() is an easy way to get up an running:
467
471
468
472
The third option runs an event loop in a separate thread.
469
473
`pubsub.run_in_thread() ` creates a new thread and starts the event loop. The
470
- thread object is returned the caller of `run_in_thread() `. The caller can
474
+ thread object is returned to the caller of `run_in_thread() `. The caller can
471
475
use the `thread.stop() ` method to shut down the event loop and thread. Behind
472
476
the scenes, this is simply a wrapper around `get_message() ` that runs in a
473
477
separate thread, essentially creating a tiny non-blocking event loop for you.
0 commit comments