0

So I found this gist on how to create a "thread pooling" system and my issue is: when the executed function finished in the thread the "completed" signal must be emitted from (or to) the main thread. The solution presented on the gist was to overwrite GObject.emit function using GObject.idle_add(GObject.GObject.emit, self, *args) which does the trick in the gist example, but misteriously does not work for me. I created a slightly modified version of his code that does what I need, except from emitting the signals correctly/at all.

class _IdleObject(GObject.GObject):
    """ Override GObject.GObject to always emit signals in the main thread by
    emmitting on an idle handler """
    @trace
    def __init__(self):
        GObject.GObject.__init__(self)

    @trace
    def emit(self, *args):
        GLib.idle_add(GObject.GObject.emit, self, *args)

In summary, the signals are either not emitted or emitted on secondary threads (when the emit function overwrite is commented out) which can't update the UI therefore don't solve my problem. Any ideas on what's going on?

My version of his gist can be found here

Paulo Queiroz
  • 123
  • 2
  • 9
  • 1
    Are you definitely running a main loop in your main thread? You say you’ve got a UI, and if that’s using GTK+ then you will have a main loop running. But this is the first thing to check. – Philip Withnall May 27 '18 at 16:45
  • Yes it's `Gtk.main` – Paulo Queiroz May 27 '18 at 22:49
  • Have you tried calling `GLib.idle_add()` with your own callback function, which prints a message when invoked? That would allow you to work out whether the problem is with the chained `GObject.GObject.emit` call, or with the idle callback itself. – Philip Withnall May 28 '18 at 12:37
  • In fact I have, the message isn't printed at all – Paulo Queiroz May 29 '18 at 13:18
  • If you print a message from your `_IdleObject.emit()` method, is that printed? – Philip Withnall May 29 '18 at 14:08
  • Duuuuude, this is so messed up, my program actually has a main loop but turns out I was testing the threads module alone (not running the main loop) LMAO. So sorry for wasting your time, and thanks a lot for the help @PhilipWithnall – Paulo Queiroz May 29 '18 at 21:34

0 Answers0