Search code examples
pythondjangoapache-kafkapykafka

TypeError: produce() got an unexpected keyword argument 'linger_ms'


I'm trying to use kafka with python with pykafka , when i try to use linger_ms i get this error :

TypeError: produce() got an unexpected keyword argument 'linger_ms'

This is my code :

import queue
from pykafka import KafkaClient
client = KafkaClient(hosts="127.0.0.1:9092,127.0.0.1:9093",broker_version="1.0.0")
topic = client.topics['mytopic']

with topic.get_producer(delivery_reports=True) as producer:
     count = 0
     while True:
         count += 1
         producer.produce(
             'test msg'.encode(encoding='UTF-8'), 
             partition_key=('{}'.format(count))
                          .encode(encoding='UTF-8'),
             timestamp=(datetime.datetime.now())+timedelta(seconds=120),
             linger_ms=120000)

Solution

  • The produce() method on a Producer does not take a linger_ms argument. That's why you get this error.

    Pass the linger_ms argument when you initialise the Producer:

    with topic.get_producer(delivery_reports=True, linger_ms=120000) as producer:
        ...