Skip to content

Commit ec0831c

Browse files
committed
wip
1 parent 55da950 commit ec0831c

2 files changed

Lines changed: 28 additions & 20 deletions

File tree

examples/consumer.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ async def consumer():
2828
tail = await stream.check_tail()
2929
logger.info("reading from tail: %s", tail)
3030
total_num_records = 0
31-
async for batch in stream.read_session(start=SeqNum(tail.seq_num)):
32-
total_num_records += len(batch.records)
33-
logger.info("read %d now, %d so far", len(batch.records), total_num_records)
31+
async with stream.read_session(start=SeqNum(tail.seq_num)) as session:
32+
async for batch in session:
33+
total_num_records += len(batch.records)
34+
logger.info(
35+
"read %d now, %d so far", len(batch.records), total_num_records
36+
)
3437

3538

3639
if __name__ == "__main__":

examples/docs/streams.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,53 +114,58 @@ async def check_tail_example(stream):
114114

115115
async 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

123124
async 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

132134
async 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

142145
async 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

155159
async 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

Comments
 (0)