我有一个在某个线程中发出信号的GObject-derived对象,我想在运行GLib的MainLoop的主线程中处理它们。
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()将在obj的线程中调用。如何在loop.run()中处理主线程中的信号
发布于 2019-02-07 17:57:22
将事件从callback信号处理程序推送到主线程的主上下文:
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 …https://stackoverflow.com/questions/54570128
复制相似问题