Search code examples
glibgobject

GObject signals and GLib MainLoop


I have an GObject-derived object that emits signals in some thread and I want to handle them in main thread, which runs GLib's MainLoop. Here is sample code that uses PyGObject:

import gi
from gi.repository import GObject, GLib

class SomeObj(GObject.Object, threading.Thread):
    def __init__(self, device_path, terminate_event):
        GObject.Object.__init__(self)
        threading.Thread.__init__(self)

    def run():
        ...
        self.emit('sig')
        ...

    @GObject.Signal
    def sig(self):
        pass

def callback(instance):
    ...
    # will be called in obj's thread

loop = GLib.MainLoop()
obj = SomeObj()
self.watcher.connect('sig', callback)
obj.start()
loop.run()

callback() will be called in obj's thread. How to handle signal in main thread inside loop.run()?


Solution

  • Push an event to the main context of the main thread from your callback signal handler:

    def callback(instance):
        # None here means the global default GMainContext, which is running in your main thread
        GLib.MainContext.invoke(None, callback_main, instance)
    
    def callback_main(instance):
        # Double check that we’re running in the main thread:
        assert(GLib.MainContext.is_owner(None))
        # … the code you want to be executed in the main thread …