Python Tkinter mainloop
Hallo Forum Hab da eine Frage: Gibt es eine Möglichkeit, die mainloop() bei Tkinter "aufzubohren" In meinem Fall sollte eine weitere "Abfragestelle" in die mainloop eingebaut sein, um anliegende Daten von pyserial abzuholen und in die Grafik "einzubauen" Die Lösung mit einem Thread scheint nicht der Weisheit bester Schluß zu sein. (?wie kann man mit sleep() etwas beschleunigen ???) wenn pyserial.inWaiting() was anzubieten hat, soll dann etwas abgeholt werden und nur dann! Nach vielen Beispielen zu Threads in Büchern und Internet folgt dann irgendwo ein "time.sleep(x.y)" um anderen Threads wieder Rechenzeit zu geben. Meiner Ansicht nach wird da mit schlafenlegen und wieder reaktivieren viel zu viel sinnlos "verbraten" Verglichen mit einem Text-widget mit Scrollbalken oder Canvas-widget mit Scrollbalken gehen vergleichbare Lösungen mit Threads sehr zäh. Die eleganteste Möglichkeit wäre m.A.n. ein "Eventhandler" für die serielle Schnittstelle. mfg Fritz
Gibt es eine Möglichkeit, die mainloop() bei Tkinter "aufzubohren" In meinem Fall sollte eine weitere "Abfragestelle" in die mainloop eingebaut sein, um anliegende Daten von pyserial abzuholen und in die Grafik "einzubauen" Die Lösung mit einem Thread scheint nicht der Weisheit bester Schluß zu sein. (?wie kann man mit sleep() etwas beschleunigen ???) wenn pyserial.inWaiting() was anzubieten hat, soll dann etwas abgeholt werden und nur dann! Nach vielen Beispielen zu Threads in Büchern und Internet folgt dann irgendwo ein "time.sleep(x.y)" um anderen Threads wieder Rechenzeit zu geben. Meiner Ansicht nach wird da mit schlafenlegen und wieder reaktivieren viel zu viel sinnlos "verbraten" Verglichen mit einem Text-widget mit Scrollbalken oder Canvas-widget mit Scrollbalken gehen vergleichbare Lösungen mit Threads sehr zäh. Die eleganteste Möglichkeit wäre m.A.n. ein "Eventhandler" für die serielle Schnittstelle.
after bzw. after_idle sind deine Freunde. Denn irgendwie musst du die aus der Verarbeitung der seriellen Daten erzeugte GUI-Aendereungen eh in den GUI-Thread bekommen. Je nach Anwendungszweck würde ich aber trotzdem einen Thread für die serielle Verarbeitung zu machen. Denn deine Überlegungen zu verbratener Zeit und der Hinweis (woher auch immer du den hast), dass man time.sleep für kooperatives Threading bräuchte, sind falsch. Wenn du in deinem serial-worker-thread im read()-call fuer den seriellen File-Deskriptor stehst, dann ist das ein blockierender Call, und damit sollte das Betriebssystem da genau keine Zeit mehr verschwenden, sondern den Thread erst dann wieder aufwecken, wenn Daten anliegen. Eine andere Möglichkeit sehe ich nicht, weil AFAIK mainloop() keinen Weg kennt, auch andere Event-Quellen (also eben "stream hat Daten") zu integrieren. Diez
participants (2)
-
Diez Roggisch
-
Fritz