Python Forum
[Tkinter] scheduling and timining issues using mqtt and tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] scheduling and timining issues using mqtt and tkinter
#3
(Apr-11-2022, 05:27 AM)deanhystad Wrote: This looks interesting.

Quote:Tkinter has its own event- and GUI-update- loop (mainloop()). Mqtts start_loop() will block Tkinters mainloop() and vice versa.

General ways to solve this are

Polling

If you can use Mqtt without its loop but with manual polling, then establish an userloop under control of Tkinters mainloop with after() method. In said userloop poll messages from mqtt.

Multithreading or multiprocessing

If you must use Mqtts loop put all mqtt stuff in a thread or process and communicate with Tkinter over Queues (unidirectional) or Pipes (bidirectional). Tkinter can handle this communication in userloop explained above in minimal intervals of few 10 ms.

What i read briefly from IMHO you have both options (Other loop...() functions are available that give a threaded interface and a manual interface)
https://pypi.org/project/paho-mqtt/

Thanks a lot for your answer, as you can see in my code I already tried multithreading (the part of my code which is commented ) and I got the errors above.
#def run_client():  
#    threading.Timer(1.0, run_client).start()  
#    client.loop()  
#def run_mqtt_client():  
#    threading.Timer(0.1, run_client).start()  
#    mqtt_client.loop_start()  
#run_client()  
#run_mqtt_client()  
The issue is that temperature, humidity... values are all sent correctly to my devices, but the commands which are sent from mydevices are received with a response error as you can see in the log I previously sent. The button from mydevices keep running (turning) without any change into the interface. When I change delays, things change and works correctly.

Error:
PUB v1/1xxxxxxxxxxxxxxxxxxxxxxxd/things/1xxxxxxxxxxxxxxxxxxxxxxxd/sys/model Python PUB v1/1xxxxxxxxxxxxxxxxxxxxxxxd/things/1xxxxxxxxxxxxxxxxxxxxxxxd/sys/version 1.1.0 Error: Unrecognised command 0 Exception in thread Thread-257: Traceback (most recent call last): File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner self.run() File "/usr/lib/python3.5/threading.py", line 1180, in run self.function(*self.args, **self.kwargs) File "test_update.py", line 216, in run_client client.loop() File "/usr/local/lib/python3.5/dist-packages/cayenne_mqtt-1.1.0-py3.5.egg/cayenne/client.py", line 122, in loop self.client.loop() File "/usr/local/lib/python3.5/dist-packages/paho_mqtt-1.4.0-py3.5.egg/paho/mqtt/client.py", line 1072, in loop rc = self.loop_read(max_packets) File "/usr/local/lib/python3.5/dist-packages/paho_mqtt-1.4.0-py3.5.egg/paho/mqtt/client.py", line 1374, in loop_read rc = self._packet_read() File "/usr/local/lib/python3.5/dist-packages/paho_mqtt-1.4.0-py3.5.egg/paho/mqtt/client.py", line 2071, in _packet_read rc = self._packet_handle() File "/usr/local/lib/python3.5/dist-packages/paho_mqtt-1.4.0-py3.5.egg/paho/mqtt/client.py", line 2568, in _packet_handle return self._handle_suback() File "/usr/local/lib/python3.5/dist-packages/paho_mqtt-1.4.0-py3.5.egg/paho/mqtt/client.py", line 2701, in _handle_suback (mid, packet) = struct.unpack(pack_format, self._in_packet['packet']) struct.error: bad char in struct format Disconnected with result code 2
Reply


Messages In This Thread
RE: scheduling and timining issues using mqtt and tkinter - by kiko4590 - Apr-11-2022, 08:23 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter - Issues with "iconbitmap" method aquerci 3 5,987 May-21-2020, 09:46 AM
Last Post: aquerci
  Tkinter Listbox tab char issues ashtona 4 4,509 Mar-27-2018, 12:28 PM
Last Post: ashtona
  Widget placement issues with tkinter grid thread 1 mgtheboss 2 4,404 Jan-09-2018, 03:59 PM
Last Post: SmokerX
  Help with tkinter Button and Label interaction issues ... sealyons 0 4,745 Jun-01-2017, 06:58 PM
Last Post: sealyons

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020