0

In my computer vision project, I want to send images from webots-controller to the AI-model as inputs and then send movements from the AI-model to webots-controller. (for the bidirectional message passing, I used two topics)

I wrote this simple code to pass messages but it don't work. What should I do?

Code

# AI-model.py
from kafka import KafkaConsumer, KafkaProducer

producer = KafkaProducer(bootstrap_servers='localhost:9092')
consumer = KafkaConsumer('model-mailbox')

while(True):
    img = consumer.__next__()
    print(img.key)
    print('a-received')

    producer.send('webots-mailbox', key=b'movement', value=b'a')
    producer.flush()
    print('a-sent')
# webots-controller.py
from kafka import KafkaConsumer, KafkaProducer

producer = KafkaProducer(bootstrap_servers='localhost:9092')
consumer = KafkaConsumer('webots-mailbox')

while True:
    producer.send('model-mailbox', key=b'image', value=b'b')
    producer.flush()
    print('b-sent')

    movement = consumer.__next__()
    print(movement.key)
    print('b-received')

Output

These are the console outputs. (I run the AI model first)

matin@matin:~/ python AI-model.py
b'image'
a-received
a-sent

As you can see, webots-controller don't receive any message.

matin@matin:~/ python webots-controller.py
b-sent

Extra x)

Also mentioning that when I comment a-received part, my messages will arrive at b-received part and console shows this output.

matin@matin:~/ python webots-controller.py
b-sent
b'movement'
b-received
Matin Zivdar
  • 593
  • 5
  • 20
  • Where is `a-done` coming from? – Svend Jul 10 '22 at 09:12
  • I changed the comment in the code, I forgot to run it after the change. I edited the question. Thanks for checking. @Svend – Matin Zivdar Jul 10 '22 at 09:15
  • I think the problem is about port, because when I use two kafka containers with different ports (one using 9092 and another 9093) It works. I'm new to kafka, I don't know what is the best way, but I think this solution isn't a good way to do bidirectional message passing. @Svend – Matin Zivdar Jul 10 '22 at 09:22
  • 1
    Kafka tends to work better for high volume events (i.e. for thing that don't have a recipient id) than one-by-one message passing. In particular the fact that it sends records by batches and that exactly-once delivery is tricky at best, the ping-pong protocol you describe might run into a bunch of corner cases. – Svend Jul 11 '22 at 06:34

0 Answers0