@@ -41,31 +41,8 @@ Note that the command methods for a pipeline always return the original
4141pipeline object, so you can "chain" several commands together, as the
4242example below shows:
4343
44- <!-- Tested examples will replace the inline ones when they are approved.
45- Markup removed to stop warnings.
46-
47- clients-example pipe_trans_tutorial basic_pipe Python
48- /clients-example
49- -->
50- ``` python
51- import redis
52-
53- r = redis.Redis(decode_responses = True )
54-
55- pipe = r.pipeline()
56-
57- for i in range (5 ):
58- pipe.set(f " seat: { i} " , f " # { i} " )
59-
60- set_5_result = pipe.execute()
61- print (set_5_result) # >>> [True, True, True, True, True]
62-
63- pipe = r.pipeline()
64-
65- # "Chain" pipeline commands together.
66- get_3_result = pipe.get(" seat:0" ).get(" seat:3" ).get(" seat:4" ).execute()
67- print (get_3_result) # >>> ['#0', '#3', '#4']
68- ```
44+ {{< clients-example pipe_trans_tutorial basic_pipe Python >}}
45+ {{< /clients-example >}}
6946
7047## Execute a transaction
7148
@@ -96,43 +73,8 @@ key is modified by another client before writing, the transaction aborts
9673with a ` WatchError ` exception, and the loop executes again for another attempt.
9774Otherwise, the loop terminates successfully.
9875
99- <!--
100- clients-example pipe_trans_tutorial trans_watch Python
101- /clients-example
102- -->
103- ``` python
104- r.set(" shellpath" , " /usr/syscmds/" )
105-
106- with r.pipeline() as pipe:
107- # Repeat until successful.
108- while True :
109- try :
110- # Watch the key we are about to change.
111- pipe.watch(" shellpath" )
112-
113- # The pipeline executes commands directly (instead of
114- # buffering them) from immediately after the `watch()`
115- # call until we begin the transaction.
116- current_path = pipe.get(" shellpath" )
117- new_path = current_path + " :/usr/mycmds/"
118-
119- # Start the transaction, which will enable buffering
120- # again for the remaining commands.
121- pipe.multi()
122-
123- pipe.set(" shellpath" , new_path)
124-
125- pipe.execute()
126-
127- # The transaction succeeded, so break out of the loop.
128- break
129- except redis.WatchError:
130- # The transaction failed, so continue with the next attempt.
131- continue
132-
133- get_path_result = r.get(" shellpath" )
134- print (get_path_result) # >>> '/usr/syscmds/:/usr/mycmds/'
135- ```
76+ {{< clients-example pipe_trans_tutorial trans_watch Python >}}
77+ {{< /clients-example >}}
13678
13779Because this is a common pattern, the library includes a convenience
13880method called ` transaction() ` that handles the code to watch keys,
@@ -144,26 +86,5 @@ using `transaction()`. Note that `transaction()` can't add the `multi()`
14486call automatically, so you must still place this correctly in your
14587transaction function.
14688
147- <!--
148- clients-example pipe_trans_tutorial watch_conv_method Python
149- /clients-example
150- *-->
151- ``` python
152- r.set(" shellpath" , " /usr/syscmds/" )
153-
154-
155- def watched_sequence (pipe ):
156- current_path = pipe.get(" shellpath" )
157- new_path = current_path + " :/usr/mycmds/"
158-
159- pipe.multi()
160-
161- pipe.set(" shellpath" , new_path)
162-
163-
164- trans_result = r.transaction(watched_sequence, " shellpath" )
165- print (trans_result) # True
166-
167- get_path_result = r.get(" shellpath" )
168- print (get_path_result) # >>> '/usr/syscmds/:/usr/mycmds/'
169- ```
89+ {{< clients-example pipe_trans_tutorial watch_conv_method Python >}}
90+ {{< /clients-example >}}
0 commit comments