import gi gi.require_version('GLib', '2.0') from gi.repository import GLib def timeout_callback(userdata): print('Timeout expired') loop = GLib.MainLoop() timeout_id = GLib.timeout_add(1000, timeout_callback, None) loop.run()
import gi gi.require_version('GLib', '2.0') from gi.repository import GLib def io_callback(fd, condition): if condition & GLib.IO_IN: data = os.read(fd, 1024) print('Data received: %s' % data.decode('utf-8')) loop = GLib.MainLoop() fd = open('/dev/input/mouse0', 'rb').fileno() io_id = GLib.io_add_watch(fd, GLib.IO_IN, io_callback) loop.run()In the above example, we create a main loop instance, then add an I/O watch event to wait for data to arrive on a file descriptor associated with a mouse device. When data is received, the `io_callback` function is called, and any received data is printed to the console.