简体   繁体   English

mqtt paho 库运行测试 docker

[英]mqtt paho library running test with docker

i've been trying to make this example running for many hours.我一直试图让这个例子运行好几个小时。 I was building an example so my friend can learn some python but i've end up frustrated on my own.我正在构建一个示例,以便我的朋友可以学习一些 python 但我自己最终感到沮丧。

My python knowledge is quite limited.我的 python 知识非常有限。 Something is causing the program thread to finish no matter how much I try delaying the execution with time.sleep (i've removed that part of the code).无论我尝试使用 time.sleep 延迟执行多少次,都会导致程序线程完成(我已经删除了那部分代码)。

Expect result: sender container should be started after the receiver one.预期结果:发送方容器应该在接收方容器之后启动。 So the receiver is subscribed to the broker and waiting for messages.所以接收者订阅了代理并等待消息。

Given result: receiver container starts and then dies.给出的结果:接收器容器启动然后死亡。

Thanks in advance.提前致谢。

I have a docker compose as follows:我有一个 docker 组成如下:

services:
  mqtt_broker:
    image: eclipse-mosquitto
    volumes:
      - "./mosquitto.conf:/mosquitto/config/mosquitto.conf"
  client_send:
    build:
      context: ./client_send/
    environment:
      BROKER_HOST: mqtt_broker
    depends_on:
      - client_receive
  client_receive:
    build:
      context: ./client_receive/
    environment:
      BROKER_HOST: mqtt_broker
    depends_on:
      - mqtt_broker

Then I have client code for each of these clients:然后我有每个客户的客户代码:

Receiver:接收者:

import os
import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    print("[receiver] Connected with result code " + str(rc))
    client.subscribe("sample_topic")

def on_message(client, userdata, msg):
    print("[receiver] got a message: " + str(msg.payload.decode()))
    client.loop_stop()

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect(os.environ["BROKER_HOST"], 1883, 60)
client.loop_start()

Sender:发件人:

import os
import paho.mqtt.client as mqtt

def run():
    print("[sender] will send a message")
    client.publish("sample_topic", "message from sender")
    client.loop_stop()

def on_connect(client, userdata, flags, rc):
    print("[sender] Connected with result code " + str(rc))
    run()

client = mqtt.Client()
client.on_connect = on_connect

client.connect(os.environ["BROKER_HOST"], 1883, 60)
client.loop_start()

I was using loop_forever without too much success bcoz print() calls were not logging anything since the main thread was blocked so I couldn't see if my code was working.我使用loop_forever并没有取得太多成功bcoz print()调用没有记录任何东西,因为主线程被阻塞所以我看不到我的代码是否工作。

EDIT: previous paragraph is just not correct.编辑:上一段是不正确的。 loop_forever will work taking this into account: Python app does not print anything when running detached in docker考虑到这一点,loop_forever 将起作用: Python 应用程序在 docker 中分离运行时不打印任何内容

Finally got it working as suggested by @Brits (see comments) just by running exit or disconnecting the client (works using exit too)终于按照@Brits 的建议(见评论)运行 exit 或断开客户端连接(也可以使用 exit )

I also keep the depends_on so the docker-compose.yml was not changed我也保留depends_on所以 docker-compose.yml 没有改变

This is the receiver:这是接收器:

import os
import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    print("[receiver] Connected with result code " + str(rc))
    client.subscribe("sample_topic")

def on_message(client, userdata, msg):
    print("[receiver] got a message: " + str(msg.payload.decode()))
    client.disconnect()

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect(os.environ["BROKER_HOST"], 1883, 60)
client.loop_forever()

As a side note, if the main thread is blocked you will never be able to see the output of the program even if you got the message back from the sender.作为旁注,如果主线程被阻塞,即使您从发件人那里收到消息,您也永远无法看到程序的 output。 If you don't disconnect the client it might actually work if your application does not rely on console output. Soo freeing the main thread allows for the system to release the output logs to docker.如果您不断开客户端,如果您的应用程序不依赖于控制台 output,它实际上可能会工作。释放主线程允许系统将 output 日志释放到 docker。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM