简体   繁体   中英

Threading in GTK3 python

I'm building a GTK GUI in Python and I need to get some data from the database which takes quite long, so the GUI freezes.

So I'm now using Threads to run the refreshing "in the background":

Thread(target=self.updateOrderList).start()

I have GUI class with alle relevant methods to manipulate the GUI. My solution work 80% of the time, but when it doesn't GTK crashed and it outputs this:

[xcb] Unknown request in queue while dequeuing
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
python3.6: ../../src/xcb_io.c:165: dequeue_pending_request:

The other times it works good, the data get loaded and its refreshing the gui.

edit: Sometimes I get this error:

Gdk-Message: 11:13:42.848: main.py: Fatal IO error 11 (Die Ressource ist zur Zeit nicht verfügbar) on X server :0

Sometimes I click the refresh button several times and it works, but then it doesn't at some point.

My main.py looks like this:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject

import gui

GObject.threads_init()

# start gui
gui.Gui()
Gtk.main()

Any ideas whats happening here?

Markus

Okay, GTK3 is not threadsafe. So I changed the program logic - doing requests in a new thread, and handling the GUI manipulation in the GUI thread ONLY . So that means I have to emit a "requests done" signal to the event loop:

Creating a new signal and registering it:

GObject.signal_new("my-custom-signal", self.window, GObject.SIGNAL_RUN_LAST, GObject.TYPE_PYOBJECT,
                       (GObject.TYPE_PYOBJECT,))
self.window.connect("my-custom-signal", self.updateOrderListCallback)

So when I click a button, start a thread:

Thread(target=self.updateOrderListThread).start()

In that thread, do the calculations, and then emit the signal:

self.window.emit("my-custom-signal", None)

so than the callback will be called after the calculations/requests/whatever are done and it works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM