Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用Tkinter&;解决性能问题;覆盆子皮上的MQTT?发布时我会被冻结6秒_Python_Tkinter_Mqtt - Fatal编程技术网

Python 如何使用Tkinter&;解决性能问题;覆盆子皮上的MQTT?发布时我会被冻结6秒

Python 如何使用Tkinter&;解决性能问题;覆盆子皮上的MQTT?发布时我会被冻结6秒,python,tkinter,mqtt,Python,Tkinter,Mqtt,下面是示例程序。它被缩减到最小的部分,以显示问题所在,即在发布主题和消息后需要很长时间的“暂停”。运行时,程序将创建一个带有两个按钮的小窗口。一个只是打印一些东西,另一个(FUNC)发布一条消息。我可以看到主题/消息到达代理,并且由于我已订阅,我可以看到消息返回。程序“冻结”约6秒,然后数据再次进入 我有另一个运行代理的RPI,还有更多的RPI在同一个网络上运行(带有固定IP的以太网)。您将在我的on_message函数中看到,我还从其他RPI读取数据,以便可以看到信息流动的速度。其他RPI以1

下面是示例程序。它被缩减到最小的部分,以显示问题所在,即在发布主题和消息后需要很长时间的“暂停”。运行时,程序将创建一个带有两个按钮的小窗口。一个只是打印一些东西,另一个(FUNC)发布一条消息。我可以看到主题/消息到达代理,并且由于我已订阅,我可以看到消息返回。程序“冻结”约6秒,然后数据再次进入

我有另一个运行代理的RPI,还有更多的RPI在同一个网络上运行(带有固定IP的以太网)。您将在我的on_message函数中看到,我还从其他RPI读取数据,以便可以看到信息流动的速度。其他RPI以1秒的间隔发送数据包

我已经在带有Buster的RPI4和带有Stretch的RPI3上运行了这个程序,其行为是相同的。在我正在创建的实际程序中(在所有模块中大约有6000行代码),程序不再只有6秒钟的“冻结”,而是冻结,不再开始运行

import tkinter as tk
from tkinter import *
import tkinter.filedialog
from tkinter import ttk
from time import sleep
################################################################################
### START OF MQTT

import paho.mqtt.client as paho
broker = "192.168.2.170"
port = 1883
Mq_C = paho.Client("RzBrain")
Mq_C.connect(broker,port)
Mq_C.loop_start()


def on_log(client, userdata, level, buf):
    print("Log: ",buf)

def on_publish(client, userdata, result):
    print("Data published \n")
    pass

def on_message(client,userdata, message):
    Topic = message.topic
    Raw_Msg = message
    try:          
        Msg = message.payload.decode("utf-8")
    except:
        print("Msg error on MQTT Read")
    if Topic == "RSV/TEMP":
        print(Msg)
    elif Topic == "RSV/HUMIDITY":
        print(Msg)
    elif Topic == "RSV/PRESSURE":
        print(Msg)
    elif Topic == "RSV/PHRASE":
        print("\n",Msg)


Mq_C.on_publish = on_publish
Mq_C.on_message = on_message

Mq_C.subscribe("RSV/TEMP")              #This data is being published on my network from another RPI
Mq_C.subscribe("RSV/HUMIDITY")      #This data is being published on my network from another RPI
Mq_C.subscribe("RSV/PRESSURE")    #This data is being published on my network from another RPI

Mq_C.subscribe("RSV/PHRASE")         # This is the return trip of a published topic / message


def Call_The_Function():
    print("Function called")
    Pay_Load = "Wuz Up"
    Mq_C.publish("RSV/PHRASE",Pay_Load)
    print("Message sent and moving on \n")

def Call_The_Print():
    print("Print Function called")
    print("Print moving on \n")


WinRoot = tk.Tk()
WinRoot.geometry('300x200+0+0')
WinRoot.title("TEST")
canvas = tk.Canvas(WinRoot, width=300, height=200, bg="gray30")
canvas.place(x=0,y=0)
MQTT_B = tk.Button(WinRoot,text="FUNC",command=Call_The_Function,bg="gray30",fg="red",height=2, width=7,highlightbackground="gray60",relief="raised",borderwidth=3)
MQTT_B.place(x=170,y=100)

Print_B = tk.Button(WinRoot,text="Print",command=Call_The_Print,bg="gray30",fg="red",height=2, width=7,highlightbackground="gray60",relief="raised",borderwidth=3)
Print_B.place(x=50,y=100)



WinRoot.mainloop()

由于您使用paho连接到您的内部IP,我认为如果没有使用示例日期,这是不可测试的。也就是说,我将花一些时间编写print语句,以确定代码锁定的位置。一旦你知道所有东西都冻结在哪里,你就可以问一个关于导致问题的方法或函数的更具体的问题。嗨,迈克,谢谢你的关注。我可能已经找到了延误的原因,但仍在调查。问题解决了!