【问题标题】:Why do io_add_watch() callbacks receive the wrong IOChannel object?为什么 io_add_watch() 回调接收到错误的 IOChannel 对象?
【发布时间】:2019-02-16 03:17:44
【问题描述】:

据我从可用文档中得知,GLib.io_add_watch() 应该注册一个在 IOChannel 上发生条件时要调用的函数,并且 callback function 应该作为第一个接收所述 IOChannel争论。太好了,除了它没有。 GLib 将一个完全不同的 IOChannel 对象传递给回调。为什么?

换句话说,为什么这段代码会产生 AssertionError?

#!/usr/bin/env python3

import gi
from gi.repository import GLib

_, _, fd, _ = GLib.spawn_async(['/bin/echo', 'hello'], standard_output=True)

channel = GLib.IOChannel.unix_new(fd)

def on_read(callback_channel, condition):
    assert callback_channel is channel

GLib.io_add_watch(channel, GLib.PRIORITY_DEFAULT, GLib.IO_IN, on_read)

GLib.MainLoop().run()

【问题讨论】:

    标签: python pygtk glib


    【解决方案1】:

    IOChannel 是一个 GBoxed 结构,而不是 GObject。它没有身份,通过副本传递。您在回调中收到的内容与您提供的内容相同,但它们不是同一个对象,它们的所有字段也不会具有相同的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-24
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-06
      • 2011-03-31
      相关资源
      最近更新 更多