File tree 4 files changed +28
-5
lines changed
4 files changed +28
-5
lines changed Original file line number Diff line number Diff line change @@ -70,3 +70,16 @@ Install the `python-snappy` module
70
70
.. code :: bash
71
71
72
72
pip install python-snappy
73
+
74
+
75
+ Optional crc32c install
76
+ ***********************
77
+ Highly recommended if you are using Kafka 11+ brokers. For those `kafka-python `
78
+ uses a new message protocol version, that requires calculation of `crc32c `,
79
+ which differs from `zlib.crc32 ` hash implementation. By default `kafka-python `
80
+ calculates it in pure python, which is quite slow. To speed it up we optionally
81
+ support https://pypi.python.org/pypi/crc32c package if it's installed.
82
+
83
+ .. code :: bash
84
+
85
+ pip install crc32c
Original file line number Diff line number Diff line change 1
1
import binascii
2
2
3
3
from kafka .record ._crc32c import crc as crc32c_py
4
+ try :
5
+ from crc32c import crc32 as crc32c_c
6
+ except ImportError :
7
+ crc32c_c = None
4
8
5
9
6
10
def encode_varint (value , write ):
@@ -113,11 +117,15 @@ def decode_varint(buffer, pos=0):
113
117
raise ValueError ("Out of int64 range" )
114
118
115
119
116
- def calc_crc32c (memview ):
120
+ _crc32c = crc32c_py
121
+ if crc32c_c is not None :
122
+ _crc32c = crc32c_c
123
+
124
+
125
+ def calc_crc32c (memview , _crc32c = _crc32c ):
117
126
""" Calculate CRC-32C (Castagnoli) checksum over a memoryview of data
118
127
"""
119
- crc = crc32c_py (memview )
120
- return crc
128
+ return _crc32c (memview )
121
129
122
130
123
131
def calc_crc32 (memview ):
Original file line number Diff line number Diff line change @@ -68,9 +68,10 @@ def test_size_of_varint(encoded, decoded):
68
68
assert util .size_of_varint (decoded ) == len (encoded )
69
69
70
70
71
- def test_crc32c ():
71
+ @pytest .mark .parametrize ("crc32_func" , [util .crc32c_c , util .crc32c_py ])
72
+ def test_crc32c (crc32_func ):
72
73
def make_crc (data ):
73
- crc = util . calc_crc32c (data )
74
+ crc = crc32_func (data )
74
75
return struct .pack (">I" , crc )
75
76
assert make_crc (b"" ) == b"\x00 \x00 \x00 \x00 "
76
77
assert make_crc (b"a" ) == b"\xc1 \xd0 \x43 \x30 "
Original file line number Diff line number Diff line change 18
18
python-snappy
19
19
lz4
20
20
xxhash
21
+ crc32c
21
22
py26: unittest2
22
23
commands =
23
24
py.test {posargs:--pylint --pylint-rcfile =pylint.rc --pylint-error-types =EF --cov =kafka --cov-config =.covrc}
You can’t perform that action at this time.
0 commit comments