@@ -114,53 +114,58 @@ async def check_tail_example(stream):
114114
115115async def read_session_example (stream ):
116116 # ANCHOR: read-session
117- async for batch in stream .read_session (start = SeqNum (0 )):
118- for record in batch .records :
119- print (f"[{ record .seq_num } ] { record .body } " )
117+ async with stream .read_session (start = SeqNum (0 )) as session :
118+ async for batch in session :
119+ for record in batch .records :
120+ print (f"[{ record .seq_num } ] { record .body } " )
120121 # ANCHOR_END: read-session
121122
122123
123124async def read_session_tail_offset (stream ):
124125 # ANCHOR: read-session-tail-offset
125126 # Start reading from 10 records before the current tail
126- async for batch in stream .read_session (start = TailOffset (10 )):
127- for record in batch .records :
128- print (f"[{ record .seq_num } ] { record .body } " )
127+ async with stream .read_session (start = TailOffset (10 )) as session :
128+ async for batch in session :
129+ for record in batch .records :
130+ print (f"[{ record .seq_num } ] { record .body } " )
129131 # ANCHOR_END: read-session-tail-offset
130132
131133
132134async def read_session_timestamp (stream ):
133135 # ANCHOR: read-session-timestamp
134136 # Start reading from a specific timestamp
135137 one_hour_ago_ms = int ((time .time () - 3600 ) * 1000 )
136- async for batch in stream .read_session (start = Timestamp (one_hour_ago_ms )):
137- for record in batch .records :
138- print (f"[{ record .seq_num } ] { record .body } " )
138+ async with stream .read_session (start = Timestamp (one_hour_ago_ms )) as session :
139+ async for batch in session :
140+ for record in batch .records :
141+ print (f"[{ record .seq_num } ] { record .body } " )
139142 # ANCHOR_END: read-session-timestamp
140143
141144
142145async def read_session_until (stream ):
143146 # ANCHOR: read-session-until
144147 # Read records until a specific timestamp
145148 one_hour_ago_ms = int ((time .time () - 3600 ) * 1000 )
146- async for batch in stream .read_session (
149+ async with stream .read_session (
147150 start = SeqNum (0 ),
148151 until_timestamp = one_hour_ago_ms ,
149- ):
150- for record in batch .records :
151- print (f"[{ record .seq_num } ] { record .body } " )
152+ ) as session :
153+ async for batch in session :
154+ for record in batch .records :
155+ print (f"[{ record .seq_num } ] { record .body } " )
152156 # ANCHOR_END: read-session-until
153157
154158
155159async def read_session_wait (stream ):
156160 # ANCHOR: read-session-wait
157161 # Read all available records, then wait up to 30 seconds for new ones
158- async for batch in stream .read_session (
162+ async with stream .read_session (
159163 start = SeqNum (0 ),
160164 wait = 30 ,
161- ):
162- for record in batch .records :
163- print (f"[{ record .seq_num } ] { record .body } " )
165+ ) as session :
166+ async for batch in session :
167+ for record in batch .records :
168+ print (f"[{ record .seq_num } ] { record .body } " )
164169 # ANCHOR_END: read-session-wait
165170
166171
0 commit comments