How to use two threads (GUI and backend)
pozz
pozzugno at gmail.com
Wed Oct 26 03:44:19 EDT 2016
Il 26/10/2016 09:13, pozz ha scritto:
> [...]
> What is the best approach to use in my scenario (GUI and backend
> communication)?
I just found this[1] page, where the thread approach is explained with
the following code:
---
import threading
import time
from gi.repository import GLib, Gtk, GObject
def app_main():
win = Gtk.Window(default_height=50, default_width=300)
win.connect("delete-event", Gtk.main_quit)
progress = Gtk.ProgressBar(show_text=True)
win.add(progress)
def update_progess(i):
progress.pulse()
progress.set_text(str(i))
return False
def example_target():
for i in range(50):
GLib.idle_add(update_progess, i)
time.sleep(0.2)
win.show_all()
thread = threading.Thread(target=example_target)
thread.daemon = True
thread.start()
if __name__ == "__main__":
# Calling GObject.threads_init() is not needed for PyGObject 3.10.2+
GObject.threads_init()
app_main()
---
This is similar to my approach, with a main difference: the callback
update_progress() added to the GLib idle loop (so executed in the main
GUI thread) receives all the data as arguments (the value i to write as
text in the progress widget).
In my case, I have many many properties of the remote device. So my
first idea is to get directly the value by accessing variables changed
during backend thread... I think this is wrong.
[1] https://wiki.gnome.org/Projects/PyGObject/Threading
More information about the Python-list
mailing list