首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GObject信号和GLib MainLoop

GObject信号和GLib MainLoop
EN

Stack Overflow用户
提问于 2019-02-07 17:29:59
回答 1查看 425关注 0票数 0

我有一个在某个线程中发出信号的GObject-derived对象,我想在运行GLibMainLoop的主线程中处理它们。

代码语言:javascript
复制
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()中处理主线程中的信号

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-07 17:57:22

将事件从callback信号处理程序推送到主线程的主上下文:

代码语言:javascript
复制
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 …
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54570128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档