Use select to poll sockets for read to reduce CPU usage (#958)

This commit is contained in:
Jianbin Wei
2017-02-03 10:06:12 -08:00
committed by Dana Powers
parent ce1bdee2ec
commit d7679681d8

View File

@@ -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: