From d7679681d8e4fff53aa4e7d9fc357ba07f8f65e4 Mon Sep 17 00:00:00 2001 From: Jianbin Wei Date: Fri, 3 Feb 2017 10:06:12 -0800 Subject: [PATCH] Use select to poll sockets for read to reduce CPU usage (#958) --- kafka/client.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/kafka/client.py b/kafka/client.py index 3de563c..46955e2 100644 --- a/kafka/client.py +++ b/kafka/client.py @@ -6,6 +6,7 @@ import functools import logging import random import time +import select from kafka.vendor import six @@ -279,6 +280,15 @@ class SimpleClient(object): conn = None while connections_by_future: futures = list(connections_by_future.keys()) + + # block until a socket is ready to be read + sockets = [ + conn._sock + for future, (conn, _) in six.iteritems(connections_by_future) + if not future.is_done and conn._sock is not None] + if sockets: + read_socks, _, _ = select.select(sockets, [], []) + for future in futures: if not future.is_done: