@@ -100,23 +100,30 @@ def __init__(self, client, group, topic, partitions=None, auto_commit=True,
100
100
self .commit )
101
101
self .commit_timer .start ()
102
102
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
+
103
113
def get_or_init_offset_callback (resp ):
104
114
try :
105
115
kafka .common .check_error (resp )
106
116
return resp .offset
107
117
except kafka .common .UnknownTopicOrPartitionError :
108
118
return 0
109
119
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 ()
120
127
121
128
def commit (self , partitions = None ):
122
129
"""
0 commit comments