@@ -41,7 +41,7 @@ or from source:
41
41
Getting Started
42
42
---------------
43
43
44
- .. code-block :: python
44
+ .. code-block :: pycon
45
45
46
46
>>> import redis
47
47
>>> r = redis.Redis(host='localhost', port=6379, db=0)
@@ -250,7 +250,7 @@ connection_pool argument of the Redis class. You may choose to do this in order
250
250
to implement client side sharding or have fine-grain control of how
251
251
connections are managed.
252
252
253
- .. code-block :: python
253
+ .. code-block :: pycon
254
254
255
255
>>> pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
256
256
>>> r = redis.Redis(connection_pool=pool)
@@ -267,7 +267,7 @@ argument, which is a string to the unix domain socket file. Additionally, make
267
267
sure the unixsocket parameter is defined in your redis.conf file. It's
268
268
commented out by default.
269
269
270
- .. code-block :: python
270
+ .. code-block :: pycon
271
271
272
272
>>> r = redis.Redis(unix_socket_path='/tmp/redis.sock')
273
273
@@ -278,7 +278,7 @@ a connection pool, passing your class to the connection_class argument.
278
278
Other keyword parameters you pass to the pool will be passed to the class
279
279
specified during initialization.
280
280
281
- .. code-block :: python
281
+ .. code-block :: pycon
282
282
283
283
>>> pool = redis.ConnectionPool(connection_class=YourConnectionClass,
284
284
your_arg='...', ...)
@@ -394,7 +394,7 @@ number of back-and-forth TCP packets between the client and server.
394
394
395
395
Pipelines are quite simple to use:
396
396
397
- .. code-block :: python
397
+ .. code-block :: pycon
398
398
399
399
>>> r = redis.Redis(...)
400
400
>>> r.set('bing', 'baz')
@@ -411,7 +411,7 @@ Pipelines are quite simple to use:
411
411
For ease of use, all commands being buffered into the pipeline return the
412
412
pipeline object itself. Therefore calls can be chained like:
413
413
414
- .. code-block :: python
414
+ .. code-block :: pycon
415
415
416
416
>>> pipe.set('foo', 'bar').sadd('faz', 'baz').incr('auto_number').execute()
417
417
[True, True, 6]
@@ -421,7 +421,7 @@ atomically as a group. This happens by default. If you want to disable the
421
421
atomic nature of a pipeline but still want to buffer commands, you can turn
422
422
off transactions.
423
423
424
- .. code-block :: python
424
+ .. code-block :: pycon
425
425
426
426
>>> pipe = r.pipeline(transaction=False)
427
427
@@ -441,7 +441,7 @@ execution of that transaction, the entire transaction will be canceled and a
441
441
WatchError will be raised. To implement our own client-side INCR command, we
442
442
could do something like this:
443
443
444
- .. code-block :: python
444
+ .. code-block :: pycon
445
445
446
446
>>> with r.pipeline() as pipe:
447
447
... while True:
@@ -474,7 +474,7 @@ Pipeline is used as a context manager (as in the example above) reset()
474
474
will be called automatically. Of course you can do this the manual way by
475
475
explicitly calling reset():
476
476
477
- .. code-block :: python
477
+ .. code-block :: pycon
478
478
479
479
>>> pipe = r.pipeline()
480
480
>>> while True:
@@ -494,7 +494,7 @@ should expect a single parameter, a pipeline object, and any number of keys to
494
494
be WATCHed. Our client-side INCR command above can be written like this,
495
495
which is much easier to read:
496
496
497
- .. code-block :: python
497
+ .. code-block :: pycon
498
498
499
499
>>> def client_side_incr(pipe):
500
500
... current_value = pipe.get('OUR-SEQUENCE-KEY')
@@ -514,15 +514,15 @@ Publish / Subscribe
514
514
redis-py includes a `PubSub ` object that subscribes to channels and listens
515
515
for new messages. Creating a `PubSub ` object is easy.
516
516
517
- .. code-block :: python
517
+ .. code-block :: pycon
518
518
519
519
>>> r = redis.Redis(...)
520
520
>>> p = r.pubsub()
521
521
522
522
Once a `PubSub ` instance is created, channels and patterns can be subscribed
523
523
to.
524
524
525
- .. code-block :: python
525
+ .. code-block :: pycon
526
526
527
527
>>> p.subscribe('my-first-channel', 'my-second-channel', ...)
528
528
>>> p.psubscribe('my-*', ...)
@@ -531,7 +531,7 @@ The `PubSub` instance is now subscribed to those channels/patterns. The
531
531
subscription confirmations can be seen by reading messages from the `PubSub `
532
532
instance.
533
533
534
- .. code-block :: python
534
+ .. code-block :: pycon
535
535
536
536
>>> p.get_message()
537
537
{'pattern': None, 'type': 'subscribe', 'channel': 'my-second-channel', 'data': 1L}
@@ -556,7 +556,7 @@ following keys.
556
556
557
557
Let's send a message now.
558
558
559
- .. code-block :: python
559
+ .. code-block :: pycon
560
560
561
561
# the publish method returns the number matching channel and pattern
562
562
# subscriptions. 'my-first-channel' matches both the 'my-first-channel'
@@ -572,7 +572,7 @@ Let's send a message now.
572
572
Unsubscribing works just like subscribing. If no arguments are passed to
573
573
[p]unsubscribe, all channels or patterns will be unsubscribed from.
574
574
575
- .. code-block :: python
575
+ .. code-block :: pycon
576
576
577
577
>>> p.unsubscribe()
578
578
>>> p.punsubscribe('my-*')
@@ -594,7 +594,7 @@ message dictionary is created and passed to the message handler. In this case,
594
594
a `None ` value is returned from get_message() since the message was already
595
595
handled.
596
596
597
- .. code-block :: python
597
+ .. code-block :: pycon
598
598
599
599
>>> def my_handler(message):
600
600
... print 'MY HANDLER: ', message['data']
@@ -620,7 +620,7 @@ subscribe/unsubscribe confirmation messages, you can ignore them by passing
620
620
subscribe/unsubscribe messages to be read, but they won't bubble up to your
621
621
application.
622
622
623
- .. code-block :: python
623
+ .. code-block :: pycon
624
624
625
625
>>> p = r.pubsub(ignore_subscribe_messages=True)
626
626
>>> p.subscribe('my-channel')
@@ -640,7 +640,7 @@ there's no data to be read, `get_message()` will immediately return None. This
640
640
makes it trivial to integrate into an existing event loop inside your
641
641
application.
642
642
643
- .. code-block :: python
643
+ .. code-block :: pycon
644
644
645
645
>>> while True:
646
646
>>> message = p.get_message()
@@ -653,7 +653,7 @@ is a generator that blocks until a message is available. If your application
653
653
doesn't need to do anything else but receive and act on messages received from
654
654
redis, listen() is an easy way to get up an running.
655
655
656
- .. code-block :: python
656
+ .. code-block :: pycon
657
657
658
658
>>> for message in p.listen():
659
659
... # do something with the message
@@ -673,7 +673,7 @@ messages that aren't automatically handled with registered message handlers.
673
673
Therefore, redis-py prevents you from calling `run_in_thread() ` if you're
674
674
subscribed to patterns or channels that don't have message handlers attached.
675
675
676
- .. code-block :: python
676
+ .. code-block :: pycon
677
677
678
678
>>> p.subscribe(**{'my-channel': my_handler})
679
679
>>> thread = p.run_in_thread(sleep_time=0.001)
@@ -697,7 +697,7 @@ reconnecting. Messages that were published while the client was disconnected
697
697
cannot be delivered. When you're finished with a PubSub object, call its
698
698
`.close() ` method to shutdown the connection.
699
699
700
- .. code-block :: python
700
+ .. code-block :: pycon
701
701
702
702
>>> p = r.pubsub()
703
703
>>> ...
@@ -707,7 +707,7 @@ cannot be delivered. When you're finished with a PubSub object, call its
707
707
The PUBSUB set of subcommands CHANNELS, NUMSUB and NUMPAT are also
708
708
supported:
709
709
710
- .. code-block :: python
710
+ .. code-block :: pycon
711
711
712
712
>>> r.pubsub_channels()
713
713
['foo', 'bar']
@@ -724,7 +724,7 @@ redis-py includes a `Monitor` object that streams every command processed
724
724
by the Redis server. Use `listen() ` on the `Monitor ` object to block
725
725
until a command is received.
726
726
727
- .. code-block :: python
727
+ .. code-block :: pycon
728
728
729
729
>>> r = redis.Redis(...)
730
730
>>> with r.monitor() as m:
@@ -747,7 +747,7 @@ The following trivial Lua script accepts two parameters: the name of a key and
747
747
a multiplier value. The script fetches the value stored in the key, multiplies
748
748
it with the multiplier value and returns the result.
749
749
750
- .. code-block :: python
750
+ .. code-block :: pycon
751
751
752
752
>>> r = redis.Redis()
753
753
>>> lua = """
@@ -769,7 +769,7 @@ function. Script instances accept the following optional arguments:
769
769
770
770
Continuing the example from above:
771
771
772
- .. code-block :: python
772
+ .. code-block :: pycon
773
773
774
774
>>> r.set('foo', 2)
775
775
>>> multiply(keys=['foo'], args=[5])
@@ -782,7 +782,7 @@ script and returns the result, 10.
782
782
Script instances can be executed using a different client instance, even one
783
783
that points to a completely different Redis server.
784
784
785
- .. code-block :: python
785
+ .. code-block :: pycon
786
786
787
787
>>> r2 = redis.Redis('redis2.example.com')
788
788
>>> r2.set('foo', 3)
@@ -798,7 +798,7 @@ passed as the client argument when calling the script. Care is taken to ensure
798
798
that the script is registered in Redis's script cache just prior to pipeline
799
799
execution.
800
800
801
- .. code-block :: python
801
+ .. code-block :: pycon
802
802
803
803
>>> pipe = r.pipeline()
804
804
>>> pipe.set('foo', 5)
@@ -816,7 +816,7 @@ in order to use redis-py's Sentinel support.
816
816
Connecting redis-py to the Sentinel instance(s) is easy. You can use a
817
817
Sentinel connection to discover the master and slaves network addresses:
818
818
819
- .. code-block :: python
819
+ .. code-block :: pycon
820
820
821
821
>>> from redis.sentinel import Sentinel
822
822
>>> sentinel = Sentinel([('localhost', 26379)], socket_timeout=0.1)
@@ -829,7 +829,7 @@ You can also create Redis client connections from a Sentinel instance. You can
829
829
connect to either the master (for write operations) or a slave (for read-only
830
830
operations).
831
831
832
- .. code-block :: python
832
+ .. code-block :: pycon
833
833
834
834
>>> master = sentinel.master_for('mymaster', socket_timeout=0.1)
835
835
>>> slave = sentinel.slave_for('mymaster', socket_timeout=0.1)
@@ -860,7 +860,7 @@ these commands are fully supported, redis-py also exposes the following methods
860
860
that return Python iterators for convenience: `scan_iter `, `hscan_iter `,
861
861
`sscan_iter ` and `zscan_iter `.
862
862
863
- .. code-block :: python
863
+ .. code-block :: pycon
864
864
865
865
>>> for key, value in (('A', '1'), ('B', '2'), ('C', '3')):
866
866
... r.set(key, value)
0 commit comments