[Kafka] Python에서 self.aync 에러 발생

    Kafka의 파이썬 코드를 실행하기 위해서 Producer 예제를 만들고 실행한 순간 에러가 발생하였다. 실행에 사용된 환경은 다음과 같다

     

    환경

    python : 3.8.3
    zookeeper : 3.6.2
    kafka : 2.6
    python kafka : kafka-1.3.5

     

    카프카 파이썬 예제

    from kafka import KafkaProducer
    
    producer = KafkaProducer(bootstrap_servers='localhost:9092')
    producer.send('test', 'Hello World!!!!')

     

    실행 에러 로그

    C:\Users\user\anaconda3\python.exe D:/phantom/source-python/main.py
    Traceback (most recent call last):
      File "D:/phantom/source-python/main.py", line 1, in <module>
        from kafka import KafkaProducer
      File "C:\Users\user\anaconda3\lib\site-packages\kafka\__init__.py", line 23, in <module>
        from kafka.producer import KafkaProducer
      File "C:\Users\user\anaconda3\lib\site-packages\kafka\producer\__init__.py", line 4, in <module>
        from .simple import SimpleProducer
      File "C:\Users\user\anaconda3\lib\site-packages\kafka\producer\simple.py", line 54
        return '<SimpleProducer batch=%s>' % self.async
                                                  ^
    SyntaxError: invalid syntax
    
    Process finished with exit code 1
    

     

    해결방법

    스택오버플로우에 나온 내용은 패키지가 잘못되었다는 것이다.

    I ran into the same problem yesterday. The answer is that this problem is actually fixed in the current version, but in PyPI, the package name "kafka" uses an older version. To get the current version, you need to switch to the package name "kafka-python".

     

    C:\Users\user>pip install kafka-python
    Collecting kafka-python
      Downloading kafka_python-2.0.2-py2.py3-none-any.whl (246 kB)
         |████████████████████████████████| 246 kB 939 kB/s
    Installing collected packages: kafka-python
    Successfully installed kafka-python-2.0.2

     

    C:\Users\user>pip uninstall kafka
    Found existing installation: kafka 1.3.5
    Uninstalling kafka-1.3.5:
      Would remove:
        d:\anaconda3\lib\site-packages\kafka-1.3.5.dist-info\*
        d:\anaconda3\lib\site-packages\kafka\*
      Would not remove (might be manually added):
        d:\anaconda3\lib\site-packages\kafka\admin\__init__.py
        d:\anaconda3\lib\site-packages\kafka\admin\acl_resource.py
        d:\anaconda3\lib\site-packages\kafka\admin\client.py
        d:\anaconda3\lib\site-packages\kafka\admin\config_resource.py
        d:\anaconda3\lib\site-packages\kafka\admin\new_partitions.py
        d:\anaconda3\lib\site-packages\kafka\admin\new_topic.py
        d:\anaconda3\lib\site-packages\kafka\coordinator\assignors\sticky\__init__.py
        d:\anaconda3\lib\site-packages\kafka\coordinator\assignors\sticky\partition_movements.py
        d:\anaconda3\lib\site-packages\kafka\coordinator\assignors\sticky\sorted_set.py
        d:\anaconda3\lib\site-packages\kafka\coordinator\assignors\sticky\sticky_assignor.py
        d:\anaconda3\lib\site-packages\kafka\oauth\__init__.py
        d:\anaconda3\lib\site-packages\kafka\oauth\abstract.py
        d:\anaconda3\lib\site-packages\kafka\protocol\parser.py
        d:\anaconda3\lib\site-packages\kafka\record\__init__.py
        d:\anaconda3\lib\site-packages\kafka\record\_crc32c.py
        d:\anaconda3\lib\site-packages\kafka\record\abc.py
        d:\anaconda3\lib\site-packages\kafka\record\default_records.py
        d:\anaconda3\lib\site-packages\kafka\record\legacy_records.py
        d:\anaconda3\lib\site-packages\kafka\record\memory_records.py
        d:\anaconda3\lib\site-packages\kafka\record\util.py
        d:\anaconda3\lib\site-packages\kafka\scram.py
        d:\anaconda3\lib\site-packages\kafka\vendor\enum34.py
    Proceed (y/n)? y
      Successfully uninstalled kafka-1.3.5

     

    카프카의 기존 버전을 삭제(uninstall)하고, kafka-python 패키지를 설치하여 실행을 하니 이제 위와 같은 에러는 나지 않고 다른 에러가 발생하였다. 다른 에러는 다음 포스팅에...

     

    참고자료

    https://github.com/dpkp/kafka-python/issues/1566

     

     

    반응형

    'Stackoverflow > Kafka' 카테고리의 다른 글

    [Kafka] Python에서 AssertionError 발생  (0) 2020.11.10

    댓글

    Designed by JB FACTORY