Skip to content

Commit 92fefb0

Browse files
committed
Modified SimpleProducer to randomize the initial round robin ordering
of partitions to prevent the first message from always being published to partition 0.
1 parent 09c053a commit 92fefb0

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

kafka/producer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import logging
44
import time
5+
import random
56

67
from Queue import Empty
78
from collections import defaultdict
@@ -197,7 +198,9 @@ def _next_partition(self, topic):
197198
if topic not in self.partition_cycles:
198199
if topic not in self.client.topic_partitions:
199200
self.client.load_metadata_for_topics(topic)
200-
self.partition_cycles[topic] = cycle(self.client.topic_partitions[topic])
201+
randomly_ordered_partitions = self.client.topic_partitions[topic][:]
202+
random.shuffle(randomly_ordered_partitions)
203+
self.partition_cycles[topic] = cycle(randomly_ordered_partitions)
201204
return self.partition_cycles[topic].next()
202205

203206
def send_messages(self, topic, *msg):

0 commit comments

Comments
 (0)