Skip to content

Commit a160361

Browse files
committed
Merge pull request #177 from zever/fetch-last-known-offsets
Move fetching last known offset logic to a stand alone function.
2 parents d73d169 + 02007b0 commit a160361

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

kafka/consumer.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,23 +100,30 @@ def __init__(self, client, group, topic, partitions=None, auto_commit=True,
100100
self.commit)
101101
self.commit_timer.start()
102102

103+
if auto_commit:
104+
self.fetch_last_known_offsets(partitions)
105+
else:
106+
for partition in partitions:
107+
self.offsets[partition] = 0
108+
109+
def fetch_last_known_offsets(self, partitions=None):
110+
if not partitions:
111+
partitions = self.client.topic_partitions[self.topic]
112+
103113
def get_or_init_offset_callback(resp):
104114
try:
105115
kafka.common.check_error(resp)
106116
return resp.offset
107117
except kafka.common.UnknownTopicOrPartitionError:
108118
return 0
109119

110-
if auto_commit:
111-
for partition in partitions:
112-
req = OffsetFetchRequest(topic, partition)
113-
(offset,) = self.client.send_offset_fetch_request(group, [req],
114-
callback=get_or_init_offset_callback,
115-
fail_on_error=False)
116-
self.offsets[partition] = offset
117-
else:
118-
for partition in partitions:
119-
self.offsets[partition] = 0
120+
for partition in partitions:
121+
req = OffsetFetchRequest(self.topic, partition)
122+
(offset,) = self.client.send_offset_fetch_request(self.group, [req],
123+
callback=get_or_init_offset_callback,
124+
fail_on_error=False)
125+
self.offsets[partition] = offset
126+
self.fetch_offsets = self.offsets.copy()
120127

121128
def commit(self, partitions=None):
122129
"""

0 commit comments

Comments
 (0)