{"id":6857,"date":"2019-02-06T13:06:04","date_gmt":"2019-02-06T18:06:04","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=6857"},"modified":"2019-02-06T13:06:47","modified_gmt":"2019-02-06T18:06:47","slug":"python-kafkaproducer-and-lost-events","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2019\/02\/06\/python-kafkaproducer-and-lost-events\/","title":{"rendered":"Python KafkaProducer and lost events"},"content":{"rendered":"\n<p>While testing the python kafka package and its associated KafkaProducer class, I found that events were &#8220;lost&#8221;; i.e., they were never successfully persisted in the destination topic.  Various blogs suggest setting a sleep(2) call, or whatever, which seemed odd.  I ran strace against the python instance, and found a couple of interesting things:<\/p>\n\n\n\n<ol><li>A few events were sent (via the sendto() system call) at a time<\/li><li>Not all events were sent<\/li><\/ol>\n\n\n\n<p>I ended up finding the flush() call, which successfully persisted the events to the topic.<\/p>\n\n\n\n<p>I also found that queue.buffering.max.ms controls how often the internal queue waits prior to writing to the topic.  batch.num.messages can also be used to control how often events are sent to the broker for persistence.<\/p>\n\n\n\n<p>As such, the code below will most likely send no events to the broker, as a loop of ten will probably finish before either parameter above is tripped.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"524\" height=\"184\" src=\"http:\/\/appcrawler.com\/wordpress\/wp-content\/uploads\/2019\/02\/image.png\" alt=\"\" class=\"wp-image-6858\" srcset=\"http:\/\/appcrawler.com\/wordpress\/wp-content\/uploads\/2019\/02\/image.png 524w, http:\/\/appcrawler.com\/wordpress\/wp-content\/uploads\/2019\/02\/image-300x105.png 300w, http:\/\/appcrawler.com\/wordpress\/wp-content\/uploads\/2019\/02\/image-260x91.png 260w, http:\/\/appcrawler.com\/wordpress\/wp-content\/uploads\/2019\/02\/image-160x56.png 160w\" sizes=\"(max-width: 524px) 100vw, 524px\" \/><figcaption><br><\/figcaption><\/figure>\n\n\n\n<p>However, adding a flush() call at the end will persist the events.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"548\" height=\"209\" src=\"http:\/\/appcrawler.com\/wordpress\/wp-content\/uploads\/2019\/02\/image-1.png\" alt=\"\" class=\"wp-image-6861\" srcset=\"http:\/\/appcrawler.com\/wordpress\/wp-content\/uploads\/2019\/02\/image-1.png 548w, http:\/\/appcrawler.com\/wordpress\/wp-content\/uploads\/2019\/02\/image-1-300x114.png 300w, http:\/\/appcrawler.com\/wordpress\/wp-content\/uploads\/2019\/02\/image-1-260x99.png 260w, http:\/\/appcrawler.com\/wordpress\/wp-content\/uploads\/2019\/02\/image-1-160x61.png 160w\" sizes=\"(max-width: 548px) 100vw, 548px\" \/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>While testing the python kafka package and its associated KafkaProducer class, I found that events were &#8220;lost&#8221;; i.e., they were never successfully persisted in the destination topic. Various blogs suggest setting a sleep(2) call, or whatever, which seemed odd. I&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2019\/02\/06\/python-kafkaproducer-and-lost-events\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[69,107],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/6857"}],"collection":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/comments?post=6857"}],"version-history":[{"count":2,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/6857\/revisions"}],"predecessor-version":[{"id":6863,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/6857\/revisions\/6863"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=6857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=6857"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=6857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}