Python3.X 執行Python編寫的生產者和消費者報錯,報錯信息如下:
Traceback (most recent call last):
File "mykit_kafka_producer.py", line 9, in <module>
from kafka import KafkaProducer
File "/usr/local/lib/python3.7/site-packages/kafka/__init__.py", line 23, in <module>
from kafka.producer import KafkaProducer
File "/usr/local/lib/python3.7/site-packages/kafka/producer/__init__.py", line 4, in <module>
from .simple import SimpleProducer
File "/usr/local/lib/python3.7/site-packages/kafka/producer/simple.py", line 54
return '<SimpleProducer batch=%s>' % self.async
^
SyntaxError: invalid syntax
原因
python3.7新增關鍵字:async、await;
kafka-python 用到了關鍵字async,由此帶來兼容性問題
解決
使用Python 3.6版本即可正常運行。
注意:先安裝 kafka ,然后安裝?kafka-python
pip install kafka
pip install kafka-python
?