question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KafkaProducer hangs when publishing to new topic (and using uWSGI master process)

See original GitHub issue

I’m trying to upgrade from SimpleProducer to the newer KafkaProducer, but I’m running into an issue where the producer hangs when it tries to publish to a topic that doesn’t exist. This occurs only when the producer is run with uWSGI, and uWSGI is configured to use a master process.

The messages are successfully published if uWSGI is run without a master process or if the topic already exists.

This behavior manifests with both kafka 0.9.0.1 and 0.10.0.

It seems like this might be related to http://stackoverflow.com/questions/37693563/how-to-make-kafka-python-or-pykafka-work-as-an-async-producer-with-uwsgi-and-gev and https://github.com/dpkp/kafka-python/issues/709, but the fact that it only breaks for nonexistent topics makes me think it might be a separate issue.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
dpkpcommented, Jun 9, 2016

Ah, thank you! Two issues:

You cannot share producer instances across processes, only threads. I expect that is why the master process pattern is failing.

Second, producer.send() is async but is not guaranteed to deliver if you close the producer abruptly. In your final example I suspect that your producer instances are so short-lived that they are being reaped before flushing all pending messages. To guarantee delivery (or exception) call producer.send().get(timeout) or producer.flush() . otherwise you’ll need to figure out how to get a producer instance per-uwsgi-thread and have it shared across requests (you would still want to flush before thread shutdown to guarantee no messages are dropped)

0reactions
zhangyufei49commented, Sep 26, 2018

uWsgi with ‘master=false’ option works fine for this scene. But I solved this by create independent producer contexts in each process. I write a ‘mq’ module

# __init__.py

from mq.MsgQueue import MsgQueue
from flask import current_app


def _init(app):
    uri = app.config['MQ_URI']
    return MsgQueue(uri)


mq_context = _init(current_app)

# MsgQueue.py

from kafka import KafkaProducer
from ujson import dumps


class MsgQueue(object):
    def __init__(self, uri):
        super(MsgQueue, self).__init__()
        self.producer = KafkaProducer(
            bootstrap_servers=uri, api_version=(0, 10, 2))

    def send(self, topic, data):
        self.producer.send(topic, dumps(data, ensure_ascii=False).encode())

And in app logic:

from mq import mq_context  # ensure only  load 'mq_context' once in each process
mq_context.send(topic, data)

pykafka also needs flow this way

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kafka producer hangs if topic doesn't exist - Stack Overflow
When the topic doesn't exist the retry for getting metadata should ends after 60 seconds by default raising a timeout exception at the...
Read more >
Configuration for the system statistics collection daemon collectd
Persistent true|false (Publish only) Selects the delivery method to use. If set to true, the persistent mode will be used, i.e. delivery is...
Read more >
Logging — uWSGI 2.0 documentation - Read the Docs
With UDP logging you can centralize cluster logging or redirect the persistence of logs to another machine to offload disk I/O. UDP logging...
Read more >
homebrew-core - Homebrew Formulae
a2ps 4.14 Any‑to‑PostScript filter aacgain 1.8 AAC‑supporting version of mp3gain aalib 1.4rc5 Portable ASCII art graphics library aamath 0.3 Renders mathematical expressions as ASCII art
Read more >
What's New - Oracle Linux Yum Server
Oracle Linux with Oracle enterprise-class support is the best Linux operating system (OS) for your enterprise computing needs.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found