Using threads in PyGObject with GTK3 in python3
<p>So I am trying to use threads to implement a blocking operation in a Python3 based application.</p>

<pre><code>#!/usr/bin/env python3

import gi, os, threading, Skype4Py
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GLib, GObject

skype = Skype4Py.Skype()

def ConnectSkype():
    skype.Attach()

class Contacts_Listbox_Row(Gtk.ListBoxRow):

    def __init__(self, name):
        # super is not a good idea, needs replacement.
        super(Gtk.ListBoxRow, self).__init__()
        self.names = name
        self.add(Gtk.Label(label=name))

class MainInterfaceWindow(Gtk.Window):
    """The Main User UI"""

    def __init__(self):
        Gtk.Window.__init__(self, title="Python-GTK-Frontend")

        # Set up Grid object
        main_grid = Gtk.Grid()
        self.add(main_grid)

        # Create a listbox which will contain selectable contacts
        contacts_listbox = Gtk.ListBox()
        for handle, name in self.GetContactTuples():
            GLib.idle_add(contacts_listbox.add, Contacts_Listbox_Row(name))
        GLib.idle_add(main_grid.add, contacts_listbox)


        # Test label for debug
        label = Gtk.Label()
        label.set_text("Test")
        GLib.idle_add(main_grid.attach_next_to, label, contacts_listbox, Gtk.PositionType.TOP, 2, 1)

    def GetContactTuples(self):
        """
        Returns a list of tuples in the form: (username, display name).
        Return -1 if failure.
        """
        print([(user.Handle, user.FullName) for user in skype.Friends]) # debug
        return [(user.Handle, user.FullName) for user in skype.Friends]

if __name__ == '__main__':

        threads = []

        thread = threading.Thread(target=ConnectSkype) # potentially blocking operation
        thread.start()
        threads.append(thread)

        main_window = MainInterfaceWindow()
        main_window.connect("delete-event", Gtk.main_quit)
        main_window.show_all()
        print('Calling Gtk.main')
        Gtk.main()
</code></pre>

<p>The basic idea is this simple program should fetch a list of contacts from the Skype API, and build a list of tuples. The <code>GetContactTuples</code> function succeeds in its design, the print call I placed verifies that. However, the program hangs indefinitely, and never renders an interface. Sometimes, it will yield random errors involving threads and/or resource availability. Once such error is </p>

<pre><code>(example.py:31248): Gdk-WARNING **: example.py: Fatal IO error 11 (Resource temporarily unavailable) on X server :1.
</code></pre>

<p>I know it is related to the use of threads, but based on the documentation <a href="https://wiki.gnome.org/Projects/PyGObject/Threading" rel="nofollow">here</a>, it seems like just adding <code>GLib.idle_add</code> calls before interface updates should be sufficient. So the questions are, why does this not work, and how could I correct the above sample?</p>

<p>UPDATE:
If <code>GLib.idle_add</code> is prepended to every line that interacts with GTK that it can be, I get a different error.
<code>
[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.
python: xcb_io.c:179: dequeue_pending_request: Assertion '!xcb_xlib_unknown_req_in_deq' failed.
Aborted (core dumped)
</code></p>