Reputation: 5
I have Python Tkinter UI based project. I send messages form textbox with mqtt publisher and listen them at the same time with the same client. It works but the infinite listening loop crashed my app after receiving message.
My problem is probably here
while 1:
client.loop(timeout=1, max_packets= 2) #contineously checking for message
Tried using loop(timeout,maxpackage) but didnt work, or i did wrong.
full part of the function
def func_sendanswer():
def func_refreshconversation():
conn = psycopg.connect("dbname=MQTTDB user=postgres host='localhost' password='pw123'")
cur = conn.cursor()
cur.execute("SELECT answercontent, creatorid, createddate FROM answers WHERE topicid ='"+ str(temp[0]) +"';")
temp2 = cur.fetchall()
text_conversation.delete("1.0","end")
for row in temp2:
text_conversation.insert(END, "\n" + "Kullanıcı ID: " + str(row[1]) + "\n" + "\n" + str(row[0]) +"\n" + "Tarih: " + str(row[2]) + "\n" + "___________________________________________________" + "\n")
cur.close()
conn.close()
message = text_sendmessage.get('0.0', 'end')
tpcname = string_topicname
print(string_topicname)
def on_message(client, userdata, message):
print("Gelen Mesaj:")
print(str(message.payload.decode("utf-8")) )
msj = str(message.payload.decode("utf-8"))
print("")
conn = psycopg.connect("dbname=MQTTDB user=postgres host='localhost' password='pw123'")
cur = conn.cursor()
cur.execute('INSERT INTO answers(answercontent, creatorid, topicid, createddate) VALUES (%s,%s,%s,current_timestamp)', (msj, online_userid, topicid))
conn.commit()
cur.close()
conn.close()
func_refreshconversation()
client= paho.Client("user") #create client object
client.on_message=on_message
print("connecting to broker host","localhost")
client.connect("localhost")#connection establishment with broker
print("subscribing begins here")
client.subscribe(tpcname)#subscribe topic test
ret= client.publish(tpcname,message) #topic name is test
while 1:
client.loop(timeout=1, max_packets= 2) #contineously checking for message
Upvotes: 0
Views: 1357
Reputation: 59628
Your model is probably wrong, Pub/Sub is not a good choice for a request/response system.
But if you really want to use it this way, then just subscribe/unsubscribe from the topic rather than try and constrain the client's network activity by messing with the loop.
Upvotes: 0