简体   繁体   English

在 paho-mqtt 中使用多个循环

[英]Using multiple loops in paho-mqtt

I am currently working on a project in which I use Paho-MQTT.我目前正在从事一个使用 Paho-MQTT 的项目。 In my code I have started a loop_forever in order to read messages from certain topics.在我的代码中,我启动了一个 loop_forever 以读取来自某些主题的消息。 The problem that is that I want to send messages with a certain delay between them to channels.问题是我想将它们之间有一定延迟的消息发送到通道。 Usually 'time.sleep(4)' works to add delay.通常 'time.sleep(4)' 可以增加延迟。 For some reason this doesn't seem to work in my code now.出于某种原因,这似乎在我的代码中不起作用。 Are there other ways to add delay in the code in this situation?在这种情况下,还有其他方法可以在代码中添加延迟吗?

This is the python code I currently have:这是我目前拥有的 python 代码:

import time
import threading
import random
from flask import Flask, request
import paho.mqtt.client as mqtt
import socket

app = Flask(__name__)

# GLOBAL VARIABLES
game = "none"
button1 = "off"
button2 = "off"
button3 = "off"
button4 = "off"

score_team_blue = 0
score_team_red = 0

# turn on all led's mqqt


def on_all():
    for i in range(1, 5):
        client.publish(str(i), "0")
        time.sleep(1)
        client.publish(str(i), "off")



# MQQT CLIENT


def on_message(client, userdata, message):
    global game
    # print topic and message
    topic = message.topic
    message = message.payload.decode("utf-8")
    print(f"Topic: {topic}, Message: {message}")
    if topic == "games":
        if message == "memory":
            game = "memory"
            print("memory")
        elif message == "redblue":
            game = "redblue"
            print("redblue")
            redvsblue()
        elif message == "zen":
            game = "zen"
            print("zen")
        elif message == "minesweepr":
            game = "minesweepr"
            print("minesweepr")
    if topic == "buttons":
        if message == "1":
            # test_first_led()
            button1 = "on"
        elif message == "2":
            button2 = "on"
        elif message == "3":
            button3 = "on"
        elif message == "4":
            button4 = "on"

        # if game == "memory":
        #     # Do read button stuff voor memory
        #     print("memory button incomming")
        # elif game == "redblue":
        #     # Do read button stuff voor redblue
        #     print("red vs blue button incomming")
        # elif game == "zen":
        #     # Do read button stuff voor zen
        #     print("zen button incomming")
        # elif game == "minesweepr":
        #     # Do read button stuff voor minesweepr
        #     print("minesweeper button incomming")

def redvsblue():
    print('red vs blue')
    for i in range(1, 5):
        client.publish(str(i), "0")
        time.sleep(1)
        client.publish(str(i), "off")

client = mqtt.Client()
client.connect("127.0.0.1", 1883)
client.on_message = on_message
# MQTT CODE to send to the web server
# Subscribe to the topic "game"
client.subscribe("games")
client.subscribe("buttons")
client.loop_forever()

while True:
    print("Starting server")
    app.run(debug=False)

loop_forever() is a blocking call, it never returns. loop_forever()是一个阻塞调用,它永远不会返回。

You can use loop_start() which will run the MQTT client.network look on a separate thread in the background.您可以使用loop_start() ,它将在后台的单独线程上运行 MQTT client.network 外观。

Please check the Paho docs for different loop options:请检查 Paho 文档以了解不同的循环选项:

https://www.eclipse.org/paho/index.php?page=clients/python/docs/index.php.network-loop https://www.eclipse.org/paho/index.php?page=clients/python/docs/index.php.network-loop

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

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